#ifndef PUGIXML_NO_XPATH
#include "common.hpp"
TEST_XML(xpath_xalan_position_1, "1234")
{
xml_node c = doc.child("doc");
CHECK_XPATH_BOOLEAN(c, STR("position()=1"), true);
CHECK_XPATH_NODESET(c, STR("*[position()=4]")) % 9;
}
TEST_XML_FLAGS(xpath_xalan_position_2, "111912632827256345", parse_default | parse_comments | parse_pi)
{
xml_node c = doc.child("doc");
CHECK_XPATH_NODESET(c, STR("*[@test and position()=8]")) % 27;
CHECK_XPATH_NODESET(c, STR("*[@test][position()=4]/num")) % 29;
CHECK_XPATH_NUMBER(c, STR("count(*)"), 8);
CHECK_XPATH_NODESET(c, STR("*[last()=position()]")) % 27;
CHECK_XPATH_NODESET(c, STR("a[position()=2]")) % 7;
CHECK_XPATH_NODESET(c, STR("a[3]/../a[position()=4]/num/../@test")) % 14;
CHECK_XPATH_BOOLEAN(c, STR("not(position()=last())"), false);
CHECK_XPATH_BOOLEAN(c, STR("position()=2"), false);
CHECK_XPATH_BOOLEAN(c, STR("last()=1"), true);
CHECK_XPATH_BOOLEAN(c, STR("last()+2=3"), true);
CHECK_XPATH_NODESET(c, STR("a[position()=5 mod 3]")) % 7;
CHECK_XPATH_NODESET(c, STR("a/num/text()[position()=1]")) % 6 % 9 % 12 % 16 % 19 % 22 % 26 % 30;
CHECK_XPATH_NODESET(c, STR("a/num/text()[position()=2]")) % 32;
CHECK_XPATH_NODESET(c, STR("a/num/text()[position()=last()]")) % 6 % 9 % 12 % 16 % 19 % 22 % 26 % 32;
CHECK_XPATH_NODESET(c, STR("a/num/text()[1]")) % 6 % 9 % 12 % 16 % 19 % 22 % 26 % 30;
CHECK_XPATH_NODESET(c, STR("a/num/text()[2]")) % 32;
CHECK_XPATH_NODESET(c, STR("a/num/text()[last()]")) % 6 % 9 % 12 % 16 % 19 % 22 % 26 % 32;
CHECK_XPATH_NODESET(c, STR("a[floor(last() div 3)]")) % 7;
CHECK_XPATH_NODESET(c, STR("a[ceiling(last() div 3)]")) % 10;
CHECK_XPATH_NODESET(c, STR("a[round(last() div 3)]")) % 10;
CHECK_XPATH_NODESET(c, STR("a[last() div 3]"));
CHECK_XPATH_NODESET(c, STR("a[last() div 2]")) % 13;
CHECK_XPATH_NODESET(c, STR("a[3]/../a[position()>=2 and position()<=4]")) % 7 % 10 % 13;
CHECK_XPATH_NUMBER(c, STR("count(a[position()>=2 and position()<=4]/num)"), 3);
CHECK_XPATH_NUMBER(c, STR("count(a/@*)"), 4);
CHECK_XPATH_NUMBER(c, STR("count(a/attribute::*)"), 4);
CHECK_XPATH_NODESET(c, STR("*[not(@test)][position()=last()]")) % 20;
CHECK_XPATH_NODESET(c, STR("*[not(@test)][last()]")) % 20;
CHECK_XPATH_NODESET(c, STR("a[3-2]")) % 3;
CHECK_XPATH_NODESET(c, STR("a[0]"));
CHECK_XPATH_NODESET(c, STR("a[9]"));
CHECK_XPATH_NODESET(c, STR("a['3']")) % 3 % 7 % 10 % 13 % 17 % 20 % 23 % 27;
CHECK_XPATH_NODESET(c, STR("a[number('3')]")) % 10;
CHECK_XPATH_NODESET(c, STR("processing-instruction()[2]")) % 34;
CHECK_XPATH_NODESET(c, STR("processing-instruction('pi')[2]")) % 34;
CHECK_XPATH_NODESET(c, STR("comment()[2]")) % 36;
CHECK_XPATH_NODESET(c, STR("a/*[last()]")) % 5 % 8 % 11 % 15 % 18 % 21 % 25 % 29;
CHECK_XPATH_NODESET(c, STR("a/child::*[last()]")) % 5 % 8 % 11 % 15 % 18 % 21 % 25 % 29;
CHECK_XPATH_NODESET(c, STR("a/descendant::*[last()]")) % 5 % 8 % 11 % 15 % 18 % 21 % 25 % 31;
CHECK_XPATH_NODESET(c, STR("a/child::node()[last()]")) % 5 % 8 % 11 % 15 % 18 % 21 % 25 % 29;
CHECK_XPATH_NODESET(c, STR("a/descendant::text()[last()]")) % 6 % 9 % 12 % 16 % 19 % 22 % 26 % 32;
CHECK_XPATH_NODESET(c, STR("child::comment()[last()]")) % 36;
}
TEST_XML(xpath_xalan_position_3, "AAABBBAbout this articleCCCThis is the section titled 'ZZZ'.DDDDon't worry.EEEThis is the deep subsection.")
{
CHECK_XPATH_NODESET(doc, STR("(article//section/title|/articleinfo/title|article/section/para)[last()]")) % 28;
CHECK_XPATH_NODESET(doc, STR("(article//section/title|/articleinfo/title|article/section/para)[1]")) % 10;
CHECK_XPATH_NUMBER(doc, STR("count(article/articleinfo/section[last()])"), 1);
CHECK_XPATH_NUMBER(doc, STR("count(article/articleinfo/section[last()][title='BBB'])"), 1);
}
TEST_XML(xpath_xalan_position_4, "")
{
CHECK_XPATH_NODESET(doc, STR("chapter//footnote[1]")) % 4 % 7 % 12;
}
TEST_XML(xpath_xalan_position_5, "")
{
CHECK_XPATH_NODESET(doc, STR("chapter//footnote[2]")) % 6 % 11 % 21 % 23;
CHECK_XPATH_NODESET(doc, STR("(chapter//footnote)[2]")) % 6;
CHECK_XPATH_NODESET(doc, STR("(child::chapter/descendant-or-self::node())/footnote[2]")) % 6 % 11 % 21 % 23;
CHECK_XPATH_NODESET(doc, STR("chapter/descendant::footnote[6]")) % 16;
CHECK_XPATH_NODESET(doc, STR("chapter/descendant::footnote[6][1][last()]")) % 16;
}
TEST_XML_FLAGS(xpath_xalan_position_6, "pcdata", parse_default | parse_pi | parse_comments)
{
CHECK_XPATH_NUMBER(doc, STR("count(/node/@attr/ancestor-or-self::node())"), 3);
CHECK_XPATH_NUMBER(doc, STR("count(/node/text()/ancestor-or-self::node())"), 4);
CHECK_XPATH_NUMBER(doc, STR("count(/node/processing-instruction()/ancestor-or-self::node())"), 4);
CHECK_XPATH_NUMBER(doc, STR("count(/node/processing-instruction('pi1')/ancestor-or-self::node())"), 3);
CHECK_XPATH_NUMBER(doc, STR("count(/node/comment()/ancestor-or-self::node())"), 3);
}
TEST_XML(xpath_xalan_position_7, "")
{
CHECK_XPATH_NODESET(doc, STR("chapter/section//@title[7]"));
CHECK_XPATH_NODESET(doc, STR("(chapter/section//@title)[7]")) % 21;
}
TEST_XML(xpath_xalan_match_1, "")
{
xml_node c = doc.child(STR("root"));
CHECK_XPATH_NODESET(c, STR("x[(position() mod 2)=1][position() > 3]")) % 21 % 27 % 33;
CHECK_XPATH_NODESET(c, STR("x[(position() mod 2)=1][position() > 3][position()=2]")) % 27;
CHECK_XPATH_NODESET(c, STR("x[(position() mod 2) > 0][position() > 3][2]")) % 27;
CHECK_XPATH_NODESET(c, STR("x[(position() mod 2)=1][position() > 3][last()]")) % 33;
CHECK_XPATH_NODESET(c, STR("x[(position() mod 2)=1][@num > 5][last()]")) % 33;
CHECK_XPATH_NODESET(c, STR("x[(@num mod 3)=2][position() > 2][last()]")) % 33;
CHECK_XPATH_NODESET(c, STR("x[(position() mod 2)=1][2][@num < 10]")) % 9;
CHECK_XPATH_NODESET(c, STR("x[(((((2*10)-4)+9) div 5) mod 3)]")) % 6;
}
TEST_XML(xpath_xalan_match_2, "doc-l1-v2doc-l1-x2doc-l1-l2-v3doc-l1-l2-w3doc-l1-l2-x3doc-l1-l2-y3doc-l1-l2-l3-v4doc-l1-l2-l3-x4")
{
CHECK_XPATH_STRING(doc, STR("doc/l1/v2"), STR("doc-l1-v2"));
CHECK_XPATH_STRING(doc, STR("doc/child::l1/x2"), STR("doc-l1-x2"));
CHECK_XPATH_STRING(doc, STR("doc/l1//v3"), STR("doc-l1-l2-v3"));
CHECK_XPATH_STRING(doc, STR("doc//l2/w3"), STR("doc-l1-l2-w3"));
CHECK_XPATH_STRING(doc, STR("doc/child::l1//x3"), STR("doc-l1-l2-x3"));
CHECK_XPATH_STRING(doc, STR("doc//child::l2/y3"), STR("doc-l1-l2-y3"));
CHECK_XPATH_STRING(doc, STR("doc//l2//v4"), STR("doc-l1-l2-l3-v4"));
CHECK_XPATH_STRING(doc, STR("doc//child::l2//x4"), STR("doc-l1-l2-l3-x4"));
CHECK_XPATH_STRING(doc, STR("doc/l1/v2"), STR("doc-l1-v2"));
CHECK_XPATH_STRING(doc, STR("doc/l1/child::x2"), STR("doc-l1-x2"));
CHECK_XPATH_STRING(doc, STR("doc/l1//v3"), STR("doc-l1-l2-v3"));
CHECK_XPATH_STRING(doc, STR("doc//l2/w3"), STR("doc-l1-l2-w3"));
CHECK_XPATH_STRING(doc, STR("doc/l1//child::x3"), STR("doc-l1-l2-x3"));
CHECK_XPATH_STRING(doc, STR("doc//l2/child::y3"), STR("doc-l1-l2-y3"));
CHECK_XPATH_STRING(doc, STR("doc//l2//v4"), STR("doc-l1-l2-l3-v4"));
CHECK_XPATH_STRING(doc, STR("doc//l2//child::x4"), STR("doc-l1-l2-l3-x4"));
CHECK_XPATH_STRING(doc, STR("doc/l1/v2"), STR("doc-l1-v2"));
CHECK_XPATH_STRING(doc, STR("doc/child::l1/child::x2"), STR("doc-l1-x2"));
CHECK_XPATH_STRING(doc, STR("doc/l1//v3"), STR("doc-l1-l2-v3"));
CHECK_XPATH_STRING(doc, STR("doc//l2/w3"), STR("doc-l1-l2-w3"));
CHECK_XPATH_STRING(doc, STR("doc/child::l1//child::x3"), STR("doc-l1-l2-x3"));
CHECK_XPATH_STRING(doc, STR("doc//child::l2/child::y3"), STR("doc-l1-l2-y3"));
CHECK_XPATH_STRING(doc, STR("doc//l2//v4"), STR("doc-l1-l2-l3-v4"));
CHECK_XPATH_STRING(doc, STR("doc//child::l2//child::x4"), STR("doc-l1-l2-l3-x4"));
}
TEST_XML(xpath_xalan_expression_1, "enENen-us")
{
CHECK_XPATH_NODESET(doc, STR("doc/para[@id='1' and lang('en')]")) % 3;
CHECK_XPATH_NODESET(doc, STR("doc/para[@id='4' and lang('en')]")) % 15;
CHECK_XPATH_NODESET(doc, STR("doc/div/para[lang('en')]")) % 9;
CHECK_XPATH_NODESET(doc, STR("doc/para[@id='3' and lang('en')]")) % 11;
CHECK_XPATH_NODESET(doc, STR("//para[lang('en')]/ancestor-or-self::*[@xml:lang]/@xml:lang")) % 5 % 8 % 13 % 17;
}
TEST_XML(xpath_xalan_predicate_1, "1234")
{
xml_node c = doc.child(STR("doc"));
CHECK_XPATH_NODESET(c, STR("a[true()=4]")) % 3 % 5 % 7 % 9;
CHECK_XPATH_NODESET(c, STR("a[true()='stringwithchars']")) % 3 % 5 % 7 % 9;
CHECK_XPATH_NODESET(c, STR("a[true()=following-sibling::*]")) % 3 % 5 % 7;
CHECK_XPATH_NODESET(c, STR("a[true()=preceding-sibling::*]")) % 5 % 7 % 9;
CHECK_XPATH_NODESET(c, STR("a[3=following-sibling::*]")) % 3 % 5;
CHECK_XPATH_NODESET(c, STR("a[0 < true()]")) % 3 % 5 % 7 % 9;
CHECK_XPATH_NODESET(c, STR("a['3.5' < 4]")) % 3 % 5 % 7 % 9;
CHECK_XPATH_NODESET(c, STR("a[3 < following-sibling::*]")) % 3 % 5 % 7;
CHECK_XPATH_NODESET(c, STR("a[following-sibling::*>3]")) % 3 % 5 % 7;
CHECK_XPATH_NODESET(c, STR("a[3 > following-sibling::*]")) % 3;
CHECK_XPATH_NODESET(c, STR("a[following-sibling::*<3]")) % 3;
CHECK_XPATH_NODESET(c, STR("a[1 < 2 < 3]")) % 3 % 5 % 7 % 9;
CHECK_XPATH_NODESET(c, STR("a[1 < 3 < 2]")) % 3 % 5 % 7 % 9;
CHECK_XPATH_NODESET(c, STR("a[following-sibling::*=true()]")) % 3 % 5 % 7;
CHECK_XPATH_NODESET(c, STR("a[false()!=following-sibling::*]")) % 3 % 5 % 7;
CHECK_XPATH_NODESET(c, STR("a[following-sibling::*!=false()]")) % 3 % 5 % 7;
CHECK_XPATH_NODESET(c, STR("a[following-sibling::*=3]")) % 3 % 5;
CHECK_XPATH_NODESET(c, STR("a[3=following-sibling::*]")) % 3 % 5;
CHECK_XPATH_NODESET(c, STR("a[4!=following-sibling::*]")) % 3 % 5;
CHECK_XPATH_NODESET(c, STR("a[following-sibling::*!=4]")) % 3 % 5;
CHECK_XPATH_NODESET(c, STR("a[3>=following-sibling::*]")) % 3 % 5;
CHECK_XPATH_NODESET(c, STR("a[3<=following-sibling::*]")) % 3 % 5 % 7;
CHECK_XPATH_NODESET(c, STR("a[following-sibling::*<=3]")) % 3 % 5;
CHECK_XPATH_NODESET(c, STR("a[following-sibling::*>=3]")) % 3 % 5 % 7;
}
TEST_XML(xpath_xalan_predicate_2, "")
{
xml_node c = doc.child(STR("foo"));
CHECK_XPATH_NODESET(c, STR("bar[@a='1' and @b='1']")) % 75 % 81 % 87 % 93;
CHECK_XPATH_NODESET(c, STR("bar[(@a='1' or @b='1') and @c='1']")) % 39 % 45 % 63 % 69 % 87 % 93;
CHECK_XPATH_NODESET(c, STR("bar[@a='1' and (@b='1' or @c='1') and @d='1']")) % 69 % 81 % 93;
CHECK_XPATH_NODESET(c, STR("bar[@a='1' and @b='1' or @c='1' and @d='1']")) % 21 % 45 % 69 % 75 % 81 % 87 % 93;
CHECK_XPATH_NODESET(c, STR("bar[(@a='1' and @b='1') or (@c='1' and @d='1')]")) % 21 % 45 % 69 % 75 % 81 % 87 % 93;
CHECK_XPATH_NODESET(c, STR("bar[@a='1' or (@b='1' and @c='1') or @d='1']")) % 9 % 21 % 33 % 39 % 45 % 51 % 57 % 63 % 69 % 75 % 81 % 87 % 93;
CHECK_XPATH_NODESET(c, STR("bar[(@a='1' or @b='1') and (@c='1' or @d='1')]")) % 33 % 39 % 45 % 57 % 63 % 69 % 81 % 87 % 93;
CHECK_XPATH_NODESET(c, STR("bar[@a='1' or @b='1' and @c='1' or @d='1']")) % 9 % 21 % 33 % 39 % 45 % 51 % 57 % 63 % 69 % 75 % 81 % 87 % 93;
CHECK_XPATH_NODESET(c, STR("bar[@a='1' or @b='1' or @c='1']")) % 15 % 21 % 27 % 33 % 39 % 45 % 51 % 57 % 63 % 69 % 75 % 81 % 87 % 93;
}
TEST_XML(xpath_xalan_predicate_3, "12345")
{
xml_node c = doc.child(STR("doc"));
CHECK_XPATH_NUMBER(c, STR("count(a[@ex])"), 2);
CHECK_XPATH_NUMBER(c, STR("count(a[@ex=''])"), 1);
CHECK_XPATH_NUMBER(c, STR("count(a[string-length(@ex)=0])"), 4);
CHECK_XPATH_NUMBER(c, STR("count(a[@ex!=''])"), 1);
CHECK_XPATH_NUMBER(c, STR("count(a[string-length(@ex) > 0])"), 1);
CHECK_XPATH_NUMBER(c, STR("count(a[not(@ex)])"), 3);
CHECK_XPATH_NUMBER(c, STR("count(a[not(@ex='')])"), 4);
CHECK_XPATH_NUMBER(c, STR("count(a[not(string-length(@ex)=0)])"), 1);
CHECK_XPATH_NUMBER(c, STR("count(a[@why='value'])"), 1);
CHECK_XPATH_NUMBER(c, STR("count(a[@why!='value'])"), 1);
}
TEST_XML(xpath_xalan_predicate_4, "
1.1 | 1.2 |
2.1 | 2.2 | 2.3 |
3.1 | 3.2 | 3.2.1 |
4 | 4.1 | 4.1.1 |
5.1 | 5.2 | 5.3 | 5.4 |
6.1 | 6.2 |
7.1 | 7.2 | 7.3 |
8.1 | 8.2 | 8.3 | 8.4 |
")
{
CHECK_XPATH_NUMBER(doc, STR("count(//tr)"), 8);
CHECK_XPATH_NUMBER(doc, STR("count(//tr[count(./td)=3])"), 2);
}
TEST_XML(xpath_xalan_predicate_5, "Wrong node selected!!Test executed successfullyWrong node selected!!")
{
CHECK_XPATH_STRING(doc, STR("doc/element1[(((((2*10)-4)+9) div 5) mod 3 )]"), STR("Test executed successfully"));
CHECK_XPATH_STRING(doc, STR("doc/element1[(((((2*10)-4)+9) div 5) mod floor(3))]"), STR("Test executed successfully"));
CHECK_XPATH_STRING(doc, STR("doc/element1[floor(2)]"), STR("Test executed successfully"));
}
TEST_XML(xpath_xalan_predicate_6, "12target34")
{
CHECK_XPATH_STRING(doc, STR("doc/a['target'=descendant::*]"), STR("2target"));
CHECK_XPATH_STRING(doc, STR("doc/a[descendant::*='target']"), STR("2target"));
}
TEST_XML(xpath_xalan_predicate_7, "12target34missed")
{
CHECK_XPATH_STRING(doc, STR("doc/a['target'!=descendant::*]"), STR("4missed"));
CHECK_XPATH_STRING(doc, STR("doc/a[descendant::*!='target']"), STR("4missed"));
}
TEST_XML(xpath_xalan_predicate_8, "this23thisthisthathellogoodbyethisthatthishellogoodbyeother")
{
xml_node c = doc.child(STR("doc"));
CHECK_XPATH_NODESET(c, STR("foo[(bar[2])='this']")) % 13;
CHECK_XPATH_NODESET(c, STR("foo[(bar[(baz[2])='goodbye'])]")) % 23 % 38;
CHECK_XPATH_NODESET(c, STR("foo[(bar[2][(baz[2])='goodbye'])]")) % 38;
}
TEST_XML(xpath_xalan_predicate_9, "f-insidef-insidef-insidef-insidef-inside")
{
CHECK_XPATH_NODESET(doc, STR("doc/*[starts-with(name(.),'f')]")) % 23;
CHECK_XPATH_NODESET(doc, STR("//*[starts-with(name(.),'f')]")) % 8 % 15 % 23 % 24;
}
TEST_XML(xpath_xalan_predicate_10, "Text from first elementText from child1 of first elementText from child2 of first elementText from second elementText from child1 of second elementText from child2 of second element (correct execution)")
{
xml_node c = doc.child(STR("doc"));
CHECK_XPATH_STRING(c, STR("//child2[ancestor::element2]"), STR("Text from child2 of second element (correct execution)"));
CHECK_XPATH_STRING(c, STR("//child2[ancestor-or-self::element2]"), STR("Text from child2 of second element (correct execution)"));
CHECK_XPATH_STRING(c, STR("//child2[attribute::attr1]"), STR("Text from child2 of second element (correct execution)"));
}
TEST_XML(xpath_xalan_predicate_11, "123456")
{
xml_node c = doc.child(STR("doc"));
CHECK_XPATH_NODESET(c, STR("a[@squeesh or (@squish and @squash)]")) % 3 % 7 % 11 % 18;
CHECK_XPATH_NODESET(c, STR("a[(@squeesh or @squish) and @squash]")) % 3 % 11;
CHECK_XPATH_NODESET(c, STR("a[@squeesh or @squish and @squash]")) % 3 % 7 % 11 % 18;
}
TEST_XML(xpath_xalan_predicate_12, "12target3target")
{
CHECK_XPATH_STRING(doc, STR("doc/a[following-sibling::*=descendant::*]"), STR("2target"));
}
TEST_XML(xpath_xalan_predicate_13, "12target3")
{
CHECK_XPATH_NODESET(doc, STR("doc/a[('target'=descendant::*) or @squish]")) % 3 % 6;
CHECK_XPATH_NODESET(doc, STR("doc/a[not(('target'=descendant::*) or @squish)]")) % 10;
}
TEST_XML(xpath_xalan_predicate_14, "12child234child4")
{
CHECK_XPATH_NODESET(doc, STR("doc/a[not(@*)]")) % 6 % 11;
}
TEST_XML(xpath_xalan_predicate_15, "xinside
")
{
CHECK_XPATH_NODESET(doc, STR("doc/descendant::*[string-length(name(.))=1]")) % 3 % 6 % 9 % 11 % 13;
}
#endif