forked from subchen/go-xmldom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.go
150 lines (129 loc) · 3.04 KB
/
query.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package xmldom
import (
"github.com/antchfx/xpath"
)
// createXPathNavigator creates a new xpath.NodeNavigator for the specified xmldom.Node.
func createXPathNavigator(top *Node) xpath.NodeNavigator {
return &xmlNodeNavigator{curr: top, attrIndex: -1}
}
// xpathQuery searches the Node that matches by the specified XPath expr.
func xpathQuery(top *Node, expr string) []*Node {
t := xpath.Select(createXPathNavigator(top), expr)
var nodes []*Node
for t.MoveNext() {
nodes = append(nodes, (t.Current().(*xmlNodeNavigator)).curr)
}
return nodes
}
// xpathQueryOne searches the Node that matches by the specified XPath expr,
// and returns first element of matched.
func xpathQueryOne(top *Node, expr string) *Node {
t := xpath.Select(createXPathNavigator(top), expr)
if t.MoveNext() {
return (t.Current().(*xmlNodeNavigator)).curr
}
return nil
}
// xpathQueryEach searches the xmldom.Node and calls functions cb.
func xpathQueryEach(top *Node, expr string, cb func(int, *Node)) {
t := xpath.Select(createXPathNavigator(top), expr)
var i int
for t.MoveNext() {
cb(i, (t.Current().(*xmlNodeNavigator)).curr)
i++
}
}
type xmlNodeNavigator struct {
curr *Node
attrIndex int
}
func (x *xmlNodeNavigator) NodeType() xpath.NodeType {
if x.curr == x.curr.Root() {
return xpath.RootNode
}
if x.attrIndex != -1 {
return xpath.AttributeNode
}
return xpath.ElementNode
}
func (x *xmlNodeNavigator) LocalName() string {
if x.attrIndex != -1 {
return x.curr.Attributes[x.attrIndex].Name
}
return x.curr.Name
}
func (x *xmlNodeNavigator) Prefix() string {
return ""
}
func (x *xmlNodeNavigator) Value() string {
if x.attrIndex != -1 {
return x.curr.Attributes[x.attrIndex].Value
}
return x.curr.Text
}
func (x *xmlNodeNavigator) Copy() xpath.NodeNavigator {
n := *x
return &n
}
func (x *xmlNodeNavigator) MoveToRoot() {
x.curr = x.curr.Root()
}
func (x *xmlNodeNavigator) MoveToParent() bool {
if node := x.curr.Parent; node != nil {
x.curr = node
return true
}
return false
}
func (x *xmlNodeNavigator) MoveToNextAttribute() bool {
if x.attrIndex >= len(x.curr.Attributes)-1 {
return false
}
x.attrIndex++
return true
}
func (x *xmlNodeNavigator) MoveToChild() bool {
if node := x.curr.FirstChild(); node != nil {
x.curr = node
return true
}
return false
}
func (x *xmlNodeNavigator) MoveToFirst() bool {
if x.curr.Parent != nil {
node := x.curr.Parent.FirstChild()
if node != nil {
x.curr = node
return true
}
}
return false
}
func (x *xmlNodeNavigator) MoveToPrevious() bool {
node := x.curr.PrevSibling()
if node != nil {
x.curr = node
return true
}
return false
}
func (x *xmlNodeNavigator) MoveToNext() bool {
node := x.curr.NextSibling()
if node != nil {
x.curr = node
return true
}
return false
}
func (x *xmlNodeNavigator) MoveTo(other xpath.NodeNavigator) bool {
node, ok := other.(*xmlNodeNavigator)
if !ok || node.curr.Root() != x.curr.Root() {
return false
}
x.curr = node.curr
x.attrIndex = node.attrIndex
return true
}
func (x *xmlNodeNavigator) String() string {
return x.Value()
}