MCPcopy Create free account
hub / github.com/antchfx/xpath / Example

Function Example

doc_test.go:168–236  ·  view source on GitHub ↗

XPath package example. See more xpath implements package: https://github.com/antchfx/htmlquery https://github.com/antchfx/xmlquery https://github.com/antchfx/jsonquery

()

Source from the content-addressed store, hash-verified

166// https://github.com/antchfx/xmlquery
167// https://github.com/antchfx/jsonquery
168func Example() {
169 /***
170 ?xml version="1.0" encoding="UTF-8"?>
171 <bookstore>
172 <book>
173 <title>Everyday Italian</title>
174 <author>Giada De Laurentiis</author>
175 <year>2005</year>
176 <price>30.00</price>
177 </book>
178 <book>
179 <title>Harry Potter</title>
180 <author>J K. Rowling</author>
181 <year>2005</year>
182 <price>29.99</price>
183 </book>
184 </bookstore>
185 **/
186
187 // Here, for begin test, we should create a document
188 books := []struct {
189 title string
190 author string
191 year int
192 price float64
193 }{
194 {title: "Everyday Italian", author: "Giada De Laurentiis", year: 2005, price: 30.00},
195 {title: "Harry Potter", author: "J K. Rowling", year: 2005, price: 29.99},
196 }
197 bookstore := &Node{Data: "bookstore", Type: ElementNode}
198 for _, v := range books {
199 book := &Node{Data: "book", Type: ElementNode}
200 title := &Node{Data: "title", Type: ElementNode}
201 title.AddChild(&Node{Data: v.title, Type: TextNode})
202 book.AddChild(title)
203 author := &Node{Data: "author", Type: ElementNode}
204 author.AddChild(&Node{Data: v.author, Type: TextNode})
205 book.AddChild(author)
206 year := &Node{Data: "year", Type: ElementNode}
207 year.AddChild(&Node{Data: fmt.Sprintf("%d", v.year), Type: TextNode})
208 book.AddChild(year)
209 price := &Node{Data: "price", Type: ElementNode}
210 price.AddChild(&Node{Data: fmt.Sprintf("%f", v.price), Type: TextNode})
211 book.AddChild(price)
212 bookstore.AddChild(book)
213 }
214 var doc = &Node{}
215 doc.AddChild(bookstore)
216 var root xpath.NodeNavigator = &NodeNavigator{curr: doc, root: doc}
217 expr, err := xpath.Compile("count(//book)")
218 // using Evaluate() method
219 if err != nil {
220 panic(err)
221 }
222 val := expr.Evaluate(root) // it returns float64 type
223 fmt.Println(val.(float64))
224
225 // using Evaluate() method

Callers

nothing calls this directly

Calls 8

AddChildMethod · 0.95
CompileFunction · 0.92
MustCompileFunction · 0.92
MoveNextMethod · 0.80
EvaluateMethod · 0.65
SelectMethod · 0.65
ValueMethod · 0.65
CurrentMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…