2
* Phoebe DOM Implementation.
4
* This is a C++ approximation of the W3C DOM model, which follows
5
* fairly closely the specifications in the various .idl files, copies of
6
* which are provided for reference. Most important is this one:
8
* http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
13
* Copyright (C) 2005 Bob Jamison
15
* This library is free software; you can redistribute it and/or
16
* modify it under the terms of the GNU Lesser General Public
17
* License as published by the Free Software Foundation; either
18
* version 2.1 of the License, or (at your option) any later version.
20
* This library is distributed in the hope that it will be useful,
21
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
* Lesser General Public License for more details.
25
* You should have received a copy of the GNU Lesser General Public
26
* License along with this library; if not, write to the Free Software
27
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34
#include "xpathparser.h"
38
using namespace org::w3c::dom;
39
using namespace org::w3c::dom::xpath;
49
XpathTest xpathTests[] =
54
"Select the root element AAA",
69
"Select all elements CCC which are children of the root element AAA",
84
"Select all elements BBB which are children of DDD which are children of the root element AAA",
99
"Select all elements BBB",
118
"Select all elements BBB which are children of DDD",
137
"Select all elements enclosed by elements /AAA/CCC/DDD",
167
"Select all elements BBB which have 3 ancestors",
197
"Select all elements",
227
"Select the first BBB child of element AAA",
238
"Select the last BBB child of element AAA",
249
"Select all attributes @id",
251
" <BBB id = 'b1'/>\n"
252
" <BBB id = 'b2'/>\n"
253
" <BBB name = 'bbb'/>\n"
260
"Select BBB elements which have attribute id",
262
" <BBB id = 'b1'/>\n"
263
" <BBB id = 'b2'/>\n"
264
" <BBB name = 'bbb'/>\n"
271
"Select BBB elements which have attribute name",
273
" <BBB id = 'b1'/>\n"
274
" <BBB id = 'b2'/>\n"
275
" <BBB name = 'bbb'/>\n"
282
"Select BBB elements which have any attribute",
284
" <BBB id = 'b1'/>\n"
285
" <BBB id = 'b2'/>\n"
286
" <BBB name = 'bbb'/>\n"
293
"Select BBB elements without an attribute",
295
" <BBB id = 'b1'/>\n"
296
" <BBB id = 'b2'/>\n"
297
" <BBB name = 'bbb'/>\n"
304
"Select BBB elements which have attribute id with value b1",
306
" <BBB id = 'b1'/>\n"
307
" <BBB name = ' bbb '/>\n"
308
" <BBB name = 'bbb'/>\n"
313
"//BBB[@name='bbb']",
314
"Select BBB elements which have attribute name with value 'bbb'",
316
" <BBB id = 'b1'/>\n"
317
" <BBB name = ' bbb '/>\n"
318
" <BBB name = 'bbb'/>\n"
323
"//BBB[normalize-space(@name)='bbb']",
324
"Select BBB elements which have attribute name with value bbb, leading and trailing spaces are removed before comparison",
326
" <BBB id = 'b1'/>\n"
327
" <BBB name = ' bbb '/>\n"
328
" <BBB name = 'bbb'/>\n"
334
"Select elements which have two children BBB",
354
"Select elements which have 2 children",
374
"Select elements which have 3 children",
394
"Select all elements with name BBB, equivalent with //BBB",
413
"//*[starts-with(name(),'B')]",
414
"Select all elements name of which starts with letter B",
433
"//*[contains(name(),'C')]",
434
"Select all elements name of which contain letter C",
453
"//*[string-length(name()) = 3]",
454
"Select elements with three-letter name",
466
"//*[string-length(name()) < 3]",
467
"Select elements name of which has one or two characters",
479
"//*[string-length(name()) > 3]",
480
"Select elements with name longer than three characters",
493
"Select all elements CCC and BBB",
506
"Select all elements BBB and elements EEE which are children of root element AAA",
518
"/AAA/EEE | //DDD/CCC | /AAA | //BBB",
519
"Number of combinations is not restricted",
532
"Equivalent of /child::AAA",
541
"Equivalent of /AAA",
550
"Equivalent of /child::AAA/child::BBB",
558
"/child::AAA/child::BBB",
559
"Equivalent of /AAA/BBB",
568
"Both possibilities can be combined",
577
"Select all descendants of document root and therefore all elements",
600
"/AAA/BBB/descendant::*",
601
"Select all descendants of /AAA/BBB",
624
"//CCC/descendant::*",
625
"Select all elements which have CCC among its ancestors",
648
"//CCC/descendant::DDD",
649
"Select elements DDD which have CCC among its ancestors",
673
"Select all parents of DDD element",
696
"/AAA/BBB/DDD/CCC/EEE/ancestor::*",
697
"Select all elements given in this absolute path",
721
"Select ancestors of FFF element",
744
"/AAA/BBB/following-sibling::*",
745
"The following-sibling axis contains all the following siblings of the context node.",
769
"//CCC/following-sibling::*",
770
"The following-sibling axis contains all the following siblings of the context node.",
794
"/AAA/XXX/preceding-sibling::*",
795
"The preceding-sibling axis contains all the preceding siblings of the context node.",
819
"//CCC/preceding-sibling::*",
820
"The preceding-sibling axis contains all the preceding siblings of the context node",
844
"/AAA/XXX/following::*",
845
"The following axis contains all nodes in the same document as the context "
846
"node that are after the context node in document order, "
847
"excluding any descendants and excluding attribute nodes and namespace nodes.",
879
"//ZZZ/following::*",
880
"The following axis contains all nodes in the same document as the context "
881
"node that are after the context node in document order, "
882
"excluding any descendants and excluding attribute nodes and namespace nodes.",
914
"/AAA/XXX/preceding::*",
915
"The preceding axis contains all nodes in the same document as the "
916
"context node that are before the context node in document order, "
917
"excluding any ancestors and excluding attribute nodes and namespace nodes",
943
"//GGG/preceding::*",
944
"The preceding axis contains all nodes in the same document as the "
945
"context node that are before the context node in document order, "
946
"excluding any ancestors and excluding attribute nodes and namespace nodes",
972
"/AAA/XXX/descendant-or-self::*",
973
"The descendant-or-self axis contains the "
974
"context node and the descendants of the context node",
1000
"//CCC/descendant-or-self::*",
1001
"The descendant-or-self axis contains the "
1002
"context node and the descendants of the context node",
1028
"/AAA/XXX/DDD/EEE/ancestor-or-self::*",
1029
"The ancestor-or-self axis contains the context node and the "
1030
"ancestors of the context node; thus, the ancestor-or-self axis "
1031
"will always include the root node.",
1057
"//GGG/ancestor-or-self::*",
1058
"The ancestor-or-self axis contains the context node and the "
1059
"ancestors of the context node; thus, the ancestor-or-self axis "
1060
"will always include the root node.",
1086
"//GGG/ancestor::*",
1087
"The ancestor, descendant, following, preceding and self axes partition a document",
1115
"//GGG/descendant::*",
1116
"The ancestor, descendant, following, preceding and self axes partition a document",
1144
"//GGG/following::*",
1145
"The ancestor, descendant, following, preceding and self axes partition a document",
1173
"//GGG/preceding::*",
1174
"The ancestor, descendant, following, preceding and self axes partition a document",
1203
"The ancestor, descendant, following, preceding and self axes partition a document",
1231
"//GGG/ancestor::* | //GGG/descendant::* | //GGG/following::* | //GGG/preceding::* | //GGG/self::*",
1232
"The ancestor, descendant, following, preceding and self axes partition a document",
1260
"//BBB[position() mod 2 = 0 ]",
1261
"Select even BBB elements",
1278
"//BBB[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ]",
1279
"Select middle BBB element(s)",
1296
"//CCC[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ]",
1297
"Select middle CCC element(s)",
1323
bool doStringTest(char *str)
1337
bool doStringTests()
1339
for (XpathTest *xpt = xpathTests ; xpt->xpathStr ; xpt++)
1341
if (!doStringTest(xpt->xpathStr))
1347
void dumpDoc(Document *doc)
1349
ls::DOMImplementationLSImpl domImpl;
1350
ls::LSSerializer &serializer = domImpl.createLSSerializer();
1351
ls::LSOutput output = domImpl.createLSOutput();
1352
io::StdWriter writer;
1353
output.setCharacterStream(&writer);
1354
serializer.write(doc, output);
1358
bool doXmlTest(XpathTest *xpt)
1361
ls::DOMImplementationLSImpl domImpl;
1362
ls::LSInput input = domImpl.createLSInput();
1363
ls::LSParser &parser = domImpl.createLSParser(0, "");
1364
input.setStringData(xpt->xml);
1365
Document *doc = parser.parse(input);
1371
org::w3c::dom::NodeList list = xp.evaluate(doc, xpt->xpathStr);
1372
for (unsigned int i=0 ; i<list.getLength() ; i++)
1374
org::w3c::dom::Node *n = list.item(i);
1385
for (XpathTest *xpt = xpathTests ; xpt->xpathStr ; xpt++)
1387
if (!doXmlTest(xpt))
1396
if (!doStringTests())
1398
printf("## Failed string tests\n");
1404
printf("## Failed xml tests\n");
1412
int main(int argc, char **argv)