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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
#ifndef PUGIXML_NO_XPATH
#include "common.hpp"
TEST_XML(xpath_paths_axes_child, "<node attr='value'><child attr='value'><subchild/></child><another/><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "child:: node()");
CHECK_XPATH_NODESET(n, "child:: node()") % 4 % 7 % 8; // child, another, last
CHECK_XPATH_NODESET(n, "another/child:: node()");
}
TEST_XML(xpath_paths_axes_descendant, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "descendant:: node()");
CHECK_XPATH_NODESET(n, "descendant:: node()") % 4 % 6 % 7 % 8 % 9; // child, subchild, another, subchild, last
CHECK_XPATH_NODESET(doc, "descendant:: node()") % 2 % 4 % 6 % 7 % 8 % 9; // node, child, subchild, another, subchild, last
CHECK_XPATH_NODESET(n, "another/descendant:: node()") % 8; // subchild
CHECK_XPATH_NODESET(n, "last/descendant:: node()");
}
TEST_XML(xpath_paths_axes_parent, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "parent:: node()");
CHECK_XPATH_NODESET(n.child("child"), "parent:: node()") % 2; // node
CHECK_XPATH_NODESET(n, "child/subchild/parent:: node()") % 4; // child
CHECK_XPATH_NODESET(n, "@attr/parent:: node()") % 2; // node
CHECK_XPATH_NODESET(n, "parent:: node()") % 1; // root
CHECK_XPATH_NODESET(doc, "parent:: node()");
}
TEST_XML(xpath_paths_axes_ancestor, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "ancestor:: node()");
CHECK_XPATH_NODESET(n.child("child"), "ancestor:: node()") % 2 % 1; // node, root
CHECK_XPATH_NODESET(n, "child/subchild/ancestor:: node()") % 4 % 2 % 1; // child, node, root
CHECK_XPATH_NODESET(n, "child/@attr/ancestor:: node()") % 4 % 2 % 1; // child, node, root
CHECK_XPATH_NODESET(n, "ancestor:: node()") % 1; // root
CHECK_XPATH_NODESET(doc, "ancestor:: node()");
}
TEST_XML(xpath_paths_axes_following_sibling, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "following-sibling:: node()");
CHECK_XPATH_NODESET(n.child("child"), "following-sibling:: node()") % 8 % 10; // another, last
CHECK_XPATH_NODESET(n.child("last"), "following-sibling:: node()");
CHECK_XPATH_NODESET(n, "@attr1/following-sibling:: node()"); // attributes are not siblings
}
TEST_XML(xpath_paths_axes_preceding_sibling, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "preceding-sibling:: node()");
CHECK_XPATH_NODESET(n.child("child"), "preceding-sibling:: node()");
CHECK_XPATH_NODESET(n.child("last"), "preceding-sibling:: node()") % 8 % 5; // another, child
CHECK_XPATH_NODESET(n, "@attr2/following-sibling:: node()"); // attributes are not siblings
}
TEST_XML(xpath_paths_axes_following, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "following:: node()");
CHECK_XPATH_NODESET(n, "following:: node()"); // no descendants
CHECK_XPATH_NODESET(n.child("child"), "following:: node()") % 8 % 9 % 10; // another, subchild, last
CHECK_XPATH_NODESET(n.child("child").child("subchild"), "following:: node()") % 8 % 9 % 10; // another, subchild, last
CHECK_XPATH_NODESET(n.child("last"), "following:: node()");
}
TEST_XML(xpath_paths_axes_preceding, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "preceding:: node()");
CHECK_XPATH_NODESET(n.child("child"), "preceding:: node()"); // no ancestors
CHECK_XPATH_NODESET(n.child("last"), "preceding:: node()") % 9 % 8 % 7 % 5; // subchild, another, subchild, child
CHECK_XPATH_NODESET(n.child("another").child("subchild"), "preceding:: node()") % 7 % 5; // subchild, child
CHECK_XPATH_NODESET(n, "preceding:: node()");
}
TEST_XML(xpath_paths_axes_attribute, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another xmlns:foo='bar'><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "attribute:: node()");
CHECK_XPATH_NODESET(n.child("child"), "attribute:: node()") % 6; // child/@attr
CHECK_XPATH_NODESET(n.child("last"), "attribute:: node()");
CHECK_XPATH_NODESET(n, "attribute:: node()") % 3 % 4; // node/@attr1 node/@attr2
CHECK_XPATH_NODESET(doc, "descendant-or-self:: node()/attribute:: node()") % 3 % 4 % 6; // all attributes
CHECK_XPATH_NODESET(n.child("another"), "attribute:: node()"); // namespace nodes are not attributes
}
TEST_XML(xpath_paths_axes_namespace, "<node xmlns:foo='bar'/>")
{
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(n, "namespace:: node()"); // namespace nodes are not supported
}
TEST_XML(xpath_paths_axes_self, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "self:: node()");
CHECK_XPATH_NODESET(n.child("child"), "self:: node()") % 4; // child
CHECK_XPATH_NODESET(n, "self:: node()") % 2; // node
CHECK_XPATH_NODESET(n, "child/@attr/self:: node()") % 5; // @attr
CHECK_XPATH_NODESET(doc, "self:: node()") % 1; // root
}
TEST_XML(xpath_paths_axes_descendant_or_self, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "descendant-or-self:: node()");
CHECK_XPATH_NODESET(n, "descendant-or-self:: node()") % 2 % 4 % 6 % 7 % 8 % 9; // node, child, subchild, another, subchild, last
CHECK_XPATH_NODESET(doc, "descendant-or-self:: node()") % 1 % 2 % 4 % 6 % 7 % 8 % 9; // root, node, child, subchild, another, subchild, last
CHECK_XPATH_NODESET(n, "another/descendant-or-self:: node()") % 7 % 8; // another, subchild
CHECK_XPATH_NODESET(n, "last/descendant-or-self:: node()") % 9; // last
}
TEST_XML(xpath_paths_axes_ancestor_or_self, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "ancestor-or-self:: node()");
CHECK_XPATH_NODESET(n.child("child"), "ancestor-or-self:: node()") % 4 % 2 % 1; // child, node, root
CHECK_XPATH_NODESET(n, "child/subchild/ancestor-or-self:: node()") % 6 % 4 % 2 % 1; // subchild, child, node, root
CHECK_XPATH_NODESET(n, "child/@attr/ancestor-or-self:: node()") % 5 % 4 % 2 % 1; // @attr, child, node, root
CHECK_XPATH_NODESET(n, "ancestor-or-self:: node()") % 2 % 1; // root, node
CHECK_XPATH_NODESET(doc, "ancestor-or-self:: node()") % 1; // root
}
TEST_XML(xpath_paths_axes_abbrev, "<node attr='value'><foo/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
// @ axis
CHECK_XPATH_NODESET(c, "@attr");
CHECK_XPATH_NODESET(n, "@attr") % 3;
// no axis - child implied
CHECK_XPATH_NODESET(c, "foo");
CHECK_XPATH_NODESET(n, "foo") % 4;
CHECK_XPATH_NODESET(doc, "node()") % 2;
// @ axis should disable all other axis specifiers
CHECK_XPATH_FAIL("@child::foo");
CHECK_XPATH_FAIL("@attribute::foo");
}
TEST_XML(xpath_paths_nodetest_all, "<node a1='v1' x:a2='v2'><c1/><x:c2/><c3/><x:c4/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "*");
CHECK_XPATH_NODESET(c, "child::*");
CHECK_XPATH_NODESET(n, "*") % 5 % 6 % 7 % 8;
CHECK_XPATH_NODESET(n, "child::*") % 5 % 6 % 7 % 8;
CHECK_XPATH_NODESET(n, "attribute::*") % 3 % 4;
}
TEST_XML_FLAGS(xpath_paths_nodetest_name, "<node a1='v1' x:a2='v2'><c1/><x:c2/><c3/><x:c4/><?c1?></node>", parse_default | parse_pi)
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "c1");
CHECK_XPATH_NODESET(c, "child::c1");
CHECK_XPATH_NODESET(n, "c1") % 5;
CHECK_XPATH_NODESET(n, "x:c2") % 6;
CHECK_XPATH_NODESET(n, "child::c1") % 5;
CHECK_XPATH_NODESET(n, "child::x:c2") % 6;
CHECK_XPATH_NODESET(n, "attribute::a1") % 3;
CHECK_XPATH_NODESET(n, "attribute::x:a2") % 4;
CHECK_XPATH_NODESET(n, "@x:a2") % 4;
}
TEST_XML(xpath_paths_nodetest_all_in_namespace, "<node a1='v1' x:a2='v2'><c1/><x:c2/><c3/><x:c4/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, "x:*");
CHECK_XPATH_NODESET(c, "child::x:*");
CHECK_XPATH_NODESET(n, "x:*") % 6 % 8;
CHECK_XPATH_NODESET(n, "child::x:*") % 6 % 8;
CHECK_XPATH_NODESET(n, "attribute::x:*") % 4;
CHECK_XPATH_NODESET(n, "@x:*") % 4;
CHECK_XPATH_FAIL(":*");
CHECK_XPATH_FAIL("@:*");
}
TEST_XML_FLAGS(xpath_paths_nodetest_type, "<node attr='value'>pcdata<child/><?pi1 value?><?pi2 value?><!--comment--><![CDATA[cdata]]></node>", parse_default | parse_pi | parse_comments)
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
// check on empty nodes
CHECK_XPATH_NODESET(c, "node()");
CHECK_XPATH_NODESET(c, "text()");
CHECK_XPATH_NODESET(c, "comment()");
CHECK_XPATH_NODESET(c, "processing-instruction()");
CHECK_XPATH_NODESET(c, "processing-instruction('foobar')");
// child axis
CHECK_XPATH_NODESET(n, "node()") % 4 % 5 % 6 % 7 % 8 % 9;
CHECK_XPATH_NODESET(n, "text()") % 4 % 9;
CHECK_XPATH_NODESET(n, "comment()") % 8;
CHECK_XPATH_NODESET(n, "processing-instruction()") % 6 % 7;
CHECK_XPATH_NODESET(n, "processing-instruction('pi2')") % 7;
// attribute axis
CHECK_XPATH_NODESET(n, "@node()") % 3;
CHECK_XPATH_NODESET(n, "@text()");
CHECK_XPATH_NODESET(n, "@comment()");
CHECK_XPATH_NODESET(n, "@processing-instruction()");
CHECK_XPATH_NODESET(n, "@processing-instruction('pi2')");
// incorrect 'argument' number
CHECK_XPATH_FAIL("node('')");
CHECK_XPATH_FAIL("text('')");
CHECK_XPATH_FAIL("comment('')");
CHECK_XPATH_FAIL("processing-instruction(1)");
CHECK_XPATH_FAIL("processing-instruction('', '')");
}
TEST_XML(xpath_paths_step_abbrev, "<node><foo/></node>")
{
doc.precompute_document_order();
xml_node c;
xml_node n = doc.child("node");
CHECK_XPATH_NODESET(c, ".");
CHECK_XPATH_NODESET(c, "..");
CHECK_XPATH_NODESET(n, ".") % 2;
CHECK_XPATH_NODESET(n, "..") % 1;
CHECK_XPATH_NODESET(n, "../node") % 2;
CHECK_XPATH_NODESET(n.child("foo"), "..") % 2;
CHECK_XPATH_FAIL(".node");
CHECK_XPATH_FAIL("..node");
}
#endif
|