Documentation ¶
Overview ¶
Example ¶
XPath package example.
package main import ( "fmt" "github.com/antchfx/xpath" ) func main() { 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()) } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // RegexpCache is a loading cache for string -> *regexp.Regexp mapping. It is exported so that in rare cases // client can customize load func and/or capacity. RegexpCache = defaultRegexpCache() )
Functions ¶
func NewLoadingCache ¶ added in v1.1.11
func NewLoadingCache(load loadFunc, capacity int) *loadingCache
NewLoadingCache creates a new instance of a loading cache with capacity. Capacity must be >= 0, or it will panic. Capacity == 0 means the cache growth is unbounded.
Types ¶
type Expr ¶
type Expr struct {
// contains filtered or unexported fields
}
Expr is an XPath expression for query.
func CompileWithNS ¶ added in v1.2.4
CompileWithNS compiles an XPath expression string, using given namespaces map.
func MustCompile ¶
MustCompile compiles an XPath expression string and ignored error.
func (*Expr) Evaluate ¶
func (expr *Expr) Evaluate(root NodeNavigator) interface{}
Evaluate returns the result of the expression. The result type of the expression is one of the follow: bool,float64,string,NodeIterator).
func (*Expr) Select ¶
func (expr *Expr) Select(root NodeNavigator) *NodeIterator
Select selects a node set using the specified XPath expression.
type NodeIterator ¶
type NodeIterator struct {
// contains filtered or unexported fields
}
NodeIterator holds all matched Node object.
func Select ¶
func Select(root NodeNavigator, expr string) *NodeIterator
Select selects a node set using the specified XPath expression. This method is deprecated, recommend using Expr.Select() method instead.
func (*NodeIterator) Current ¶
func (t *NodeIterator) Current() NodeNavigator
Current returns current node which matched.
func (*NodeIterator) MoveNext ¶
func (t *NodeIterator) MoveNext() bool
MoveNext moves Navigator to the next match node.
type NodeNavigator ¶
type NodeNavigator interface { NodeType LocalName() string Prefix() string Value() string Copy() NodeNavigator MoveToRoot() MoveToParent() bool MoveToNextAttribute() bool MoveToChild() bool MoveToFirst() bool MoveToNext() bool MoveToPrevious() bool MoveTo(NodeNavigator) bool }NodeType()
NodeNavigator provides cursor model for navigating XML data.
type NodeType ¶
type NodeType int
NodeType represents a type of XPath node.
const ( // RootNode is a root node of the XML document or node tree. RootNode NodeType = iota // ElementNode is an element, such as <element>. ElementNode // AttributeNode is an attribute, such as id='123'. AttributeNode // TextNode is the text content of a node. TextNode // CommentNode is a comment node, such as <!-- my comment --> CommentNode )