| 1 |
package xpath |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
) |
| 7 |
|
| 8 |
// NodeType represents a type of XPath node. |
| 9 |
type NodeType int |
| 10 |
|
| 11 |
const ( |
| 12 |
// RootNode is a root node of the XML document or node tree. |
| 13 |
RootNode NodeType = iota |
| 14 |
|
| 15 |
// ElementNode is an element, such as <element>. |
| 16 |
ElementNode |
| 17 |
|
| 18 |
// AttributeNode is an attribute, such as id='123'. |
| 19 |
AttributeNode |
| 20 |
|
| 21 |
// TextNode is the text content of a node. |
| 22 |
TextNode |
| 23 |
|
| 24 |
// CommentNode is a comment node, such as <!-- my comment --> |
| 25 |
CommentNode |
| 26 |
|
| 27 |
// allNode is any types of node, used by xpath package only to predicate match. |
| 28 |
allNode |
| 29 |
) |
| 30 |
|
| 31 |
// NodeNavigator provides cursor model for navigating XML data. |
| 32 |
type NodeNavigator interface { |
| 33 |
// NodeType returns the XPathNodeType of the current node. |
| 34 |
NodeType() NodeType |
| 35 |
|
| 36 |
// LocalName gets the Name of the current node. |
| 37 |
LocalName() string |
| 38 |
|
| 39 |
// Prefix returns namespace prefix associated with the current node. |
| 40 |
Prefix() string |
| 41 |
|
| 42 |
// Value gets the value of current node. |
| 43 |
Value() string |
| 44 |
|
| 45 |
// Copy does a deep copy of the NodeNavigator and all its components. |
| 46 |
Copy() NodeNavigator |
| 47 |
|
| 48 |
// MoveToRoot moves the NodeNavigator to the root node of the current node. |
| 49 |
MoveToRoot() |
| 50 |
|
| 51 |
// MoveToParent moves the NodeNavigator to the parent node of the current node. |
| 52 |
MoveToParent() bool |
| 53 |
|
| 54 |
// MoveToNextAttribute moves the NodeNavigator to the next attribute on current node. |
| 55 |
MoveToNextAttribute() bool |
| 56 |
|
| 57 |
// MoveToChild moves the NodeNavigator to the first child node of the current node. |
| 58 |
MoveToChild() bool |
| 59 |
|
| 60 |
// MoveToFirst moves the NodeNavigator to the first sibling node of the current node. |
| 61 |
MoveToFirst() bool |
| 62 |
|
| 63 |
// MoveToNext moves the NodeNavigator to the next sibling node of the current node. |
| 64 |
MoveToNext() bool |
| 65 |
|
| 66 |
// MoveToPrevious moves the NodeNavigator to the previous sibling node of the current node. |
| 67 |
MoveToPrevious() bool |
| 68 |
|
| 69 |
// MoveTo moves the NodeNavigator to the same position as the specified NodeNavigator. |
| 70 |
MoveTo(NodeNavigator) bool |
| 71 |
} |
| 72 |
|
| 73 |
// NodeIterator holds all matched Node object. |
| 74 |
type NodeIterator struct { |
| 75 |
node NodeNavigator |
| 76 |
query query |
| 77 |
} |
| 78 |
|
| 79 |
// Current returns current node which matched. |
| 80 |
func (t *NodeIterator) Current() NodeNavigator { |
| 81 |
return t.node |
| 82 |
} |
| 83 |
|
| 84 |
// MoveNext moves Navigator to the next match node. |
| 85 |
func (t *NodeIterator) MoveNext() bool { |
| 86 |
n := t.query.Select(t) |
| 87 |
if n == nil { |
| 88 |
return false |
| 89 |
} |
| 90 |
if !t.node.MoveTo(n) { |
| 91 |
t.node = n.Copy() |
| 92 |
} |
| 93 |
return true |
| 94 |
} |
| 95 |
|
| 96 |
// Select selects a node set using the specified XPath expression. |
| 97 |
// This method is deprecated, recommend using Expr.Select() method instead. |
| 98 |
func Select(root NodeNavigator, expr string) *NodeIterator { |
| 99 |
exp, err := Compile(expr) |
| 100 |
if err != nil { |
| 101 |
panic(err) |
| 102 |
} |
| 103 |
return exp.Select(root) |
| 104 |
} |
| 105 |
|
| 106 |
// Expr is an XPath expression for query. |
| 107 |
type Expr struct { |
| 108 |
s string |
| 109 |
q query |
| 110 |
} |
| 111 |
|
| 112 |
type iteratorFunc func() NodeNavigator |
| 113 |
|
| 114 |
func (f iteratorFunc) Current() NodeNavigator { |
| 115 |
return f() |
| 116 |
} |
| 117 |
|
| 118 |
// Evaluate returns the result of the expression. |
| 119 |
// The result type of the expression is one of the follow: bool,float64,string,NodeIterator). |
| 120 |
func (expr *Expr) Evaluate(root NodeNavigator) interface{} { |
| 121 |
val := expr.q.Evaluate(iteratorFunc(func() NodeNavigator { return root })) |
| 122 |
switch val.(type) { |
| 123 |
case query: |
| 124 |
return &NodeIterator{query: expr.q.Clone(), node: root} |
| 125 |
} |
| 126 |
return val |
| 127 |
} |
| 128 |
|
| 129 |
// Select selects a node set using the specified XPath expression. |
| 130 |
func (expr *Expr) Select(root NodeNavigator) *NodeIterator { |
| 131 |
return &NodeIterator{query: expr.q.Clone(), node: root} |
| 132 |
} |
| 133 |
|
| 134 |
// String returns XPath expression string. |
| 135 |
func (expr *Expr) String() string { |
| 136 |
return expr.s |
| 137 |
} |
| 138 |
|
| 139 |
// Compile compiles an XPath expression string. |
| 140 |
func Compile(expr string) (*Expr, error) { |
| 141 |
if expr == "" { |
| 142 |
return nil, errors.New("expr expression is nil") |
| 143 |
} |
| 144 |
qy, err := build(expr, nil) |
| 145 |
if err != nil { |
| 146 |
return nil, err |
| 147 |
} |
| 148 |
if qy == nil { |
| 149 |
return nil, fmt.Errorf(fmt.Sprintf("undeclared variable in XPath expression: %s", expr)) |
| 150 |
} |
| 151 |
return &Expr{s: expr, q: qy}, nil |
| 152 |
} |
| 153 |
|
| 154 |
// MustCompile compiles an XPath expression string and ignored error. |
| 155 |
func MustCompile(expr string) *Expr { |
| 156 |
exp, err := Compile(expr) |
| 157 |
if err != nil { |
| 158 |
return &Expr{s: expr, q: nopQuery{}} |
| 159 |
} |
| 160 |
return exp |
| 161 |
} |
| 162 |
|
| 163 |
// CompileWithNS compiles an XPath expression string, using given namespaces map. |
| 164 |
func CompileWithNS(expr string, namespaces map[string]string) (*Expr, error) { |
| 165 |
if expr == "" { |
| 166 |
return nil, errors.New("expr expression is nil") |
| 167 |
} |
| 168 |
qy, err := build(expr, namespaces) |
| 169 |
if err != nil { |
| 170 |
return nil, err |
| 171 |
} |
| 172 |
if qy == nil { |
| 173 |
return nil, fmt.Errorf(fmt.Sprintf("undeclared variable in XPath expression: %s", expr)) |
| 174 |
} |
| 175 |
return &Expr{s: expr, q: qy}, nil |
| 176 |
} |