Skip to content

Commit ef0ed92

Browse files
committed
remove books.xml and update docs example
1 parent 25aab14 commit ef0ed92

File tree

2 files changed

+64
-121
lines changed

2 files changed

+64
-121
lines changed

books.xml

Lines changed: 0 additions & 121 deletions
This file was deleted.

doc_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package xmlquery_test
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/antchfx/xmlquery"
8+
"github.com/antchfx/xpath"
9+
)
10+
11+
func Example() {
12+
// XPATH syntax and functions see https://github.com/antchfx/xpath
13+
s := `<?xml version="1.0"?>
14+
<bookstore>
15+
<book id="bk101">
16+
<author>Gambardella, Matthew</author>
17+
<title>XML Developer's Guide</title>
18+
<genre>Computer</genre>
19+
<price>44.95</price>
20+
<publish_date>2000-10-01</publish_date>
21+
</book>
22+
<book id="bk102">
23+
<author>Ralls, Kim</author>
24+
<title>Midnight Rain</title>
25+
<genre>Fantasy</genre>
26+
<price>5.95</price>
27+
<publish_date>2000-12-16</publish_date>
28+
</book>
29+
<book id="bk103">
30+
<author>Corets, Eva</author>
31+
<title>Maeve Ascendant</title>
32+
<genre>Fantasy</genre>
33+
<price>5.95</price>
34+
<publish_date>2000-11-17</publish_date>
35+
</book>
36+
</bookstore>`
37+
38+
doc, err := xmlquery.Parse(strings.NewReader(s))
39+
if err != nil {
40+
panic(err)
41+
}
42+
// Quick query all books.
43+
books := xmlquery.Find(doc, `/bookstore/book`)
44+
for _, n := range books {
45+
fmt.Println(n)
46+
}
47+
48+
// Find all books with price rather than 10.
49+
books = xmlquery.Find(doc, `//book[price < 10]`)
50+
fmt.Println(len(books))
51+
52+
// Find books with @id=bk102 or @id=bk101
53+
books = xmlquery.Find(doc, `//book[@id = "bk102" or @id = "bk101"]`)
54+
fmt.Println(len(books))
55+
56+
// Find books by author: Corets, Eva
57+
book := xmlquery.FindOne(doc, `//book[author = "Corets, Eva"]`)
58+
fmt.Println(book.SelectElement("title").InnerText()) // > Output: Maeve Ascendant
59+
60+
// Calculate the total prices of all books
61+
nav := xmlquery.CreateXPathNavigator(doc)
62+
prices := xpath.MustCompile(`sum(//book/price)`).Evaluate(nav).(float64)
63+
fmt.Println(prices) // > Output: 56.85
64+
}

0 commit comments

Comments
 (0)