goProject/trunk/goutil/xmlUtil/gxpath/select.go

41 lines
850 B
Go
Raw Permalink Normal View History

2025-01-06 16:01:02 +08:00
package gxpath
import (
"goutil/xmlUtil/gxpath/internal/build"
"goutil/xmlUtil/gxpath/internal/query"
"goutil/xmlUtil/gxpath/xpath"
)
// NodeIterator holds all matched Node object.
type NodeIterator struct {
node xpath.NodeNavigator
query query.Query
}
// Current returns current node which matched.
func (t *NodeIterator) Current() xpath.NodeNavigator {
return t.node
}
// MoveNext moves Navigator to the next match node.
func (t *NodeIterator) MoveNext() bool {
n := t.query.Select(t)
if n != nil {
if !t.node.MoveTo(n) {
t.node = n.Copy()
}
return true
}
return false
}
// Select selects a node set using the specified XPath expression.
func Select(root xpath.NodeNavigator, expr string) *NodeIterator {
qy, err := build.Build(expr)
if err != nil {
panic(err)
}
t := &NodeIterator{query: qy, node: root}
return t
}