File: doc_test.go

package info (click to toggle)
golang-github-antchfx-xpath 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: makefile: 2
file content (33 lines) | stat: -rw-r--r-- 715 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package xpath_test

import (
	"fmt"

	"github.com/antchfx/xpath"
)

// XPath package example.
func Example() {
	expr, err := xpath.Compile("count(//book)")
	if err != nil {
		panic(err)
	}
	var root xpath.NodeNavigator
	// using Evaluate() method
	val := expr.Evaluate(root) // it returns float64 type
	fmt.Println(val.(float64))

	// using Evaluate() method
	expr = xpath.MustCompile("//book")
	val = expr.Evaluate(root) // it returns NodeIterator type.
	iter := val.(*xpath.NodeIterator)
	for iter.MoveNext() {
		fmt.Println(iter.Current().Value())
	}

	// using Select() method
	iter = expr.Select(root) // it always returns NodeIterator object.
	for iter.MoveNext() {
		fmt.Println(iter.Current().Value())
	}
}