TinyXPath examples / regression tests
Input XML tree
<a><b val='123'>
<b />
<c />
<!-- -122.0 -->
<d />
</b>
<!-- 500.0 -->
<x target='xyz'>
sub text </x>
</a>
Expression | Result | Expected (compiled) |
---|
Results
@val 123 123//x/text() sub text sub text//*/comment() -122.0 -122.0 count(//*/comment()) 2 2sum(//@*) 123 123sum(//*/comment()) 378 378true() true truenot(false()) true truecount(//*[position()=2]) 2 2name(/*/*/*[position()=2]) c cname(/*/*/*[last()]) d dcount(//c/following::*) 2 2count(/a/b/b/following::*) 3 3count(//d/preceding::*) 2 2name(//attribute::*) val valcount(//b/child::*) 3 3count(//x/ancestor-or-self::*) 2 2count(//b/descendant-or-self::*) 4 4count(//self::*) 6 6count(/a/descendant::*) 5 5count(/a/descendant::x) 1 1count(/a/descendant::b) 2 2count(/a/descendant::b[@val=123]) 1 1count(//c/ancestor::a) 1 1name(//d/parent::*) b bcount(//c/ancestor::*) 2 2name(/a/b/ancestor::*) a aname(/a/b/c/following-sibling::*) d dcount(//b/following-sibling::*) 3 3count(//b|//a) 3 3count(//d/preceding-sibling::*) 2 2-3 * 4 -12 -12-3.1 * 4 -12.4 -12.412 div 5 2.4 2.43 * 7 21 21-5.5 >= -5.5 true true-5.5 < 3 true true-6.0 < -7 false false12 < 14 true true12 > 14 false false14 <= 14 true true/a or /b true true/c or /b false false/a and /b false false/a and /*/b true true18-12 6 618+12 30 30count(//a|//b) 3 3count(//*[@val]) 1 1name(//*[@val=123]) b b3=4 false false3!=4 true true12=12 true true'here is a string'='here is a string' true true'here is a string'!='here is a string' false false/a/b/@val 123 123count(//*/b) 2 2name(/*/*/*[2]) c cname(/*) a aname(/a) a aname(/a/b) b bname(/*/*) b bname(/a/b/c) c ccount(/a/b/*) 3 3ceiling(3.5) 4 4concat('first ','second',' third','') first second third first second thirdceiling(5) 5 5floor(3.5) 3 3floor(5) 5 5string-length('try') 3 3concat(name(/a/b[1]/*[1]),' ',name(/a/b/*[2])) b c b ccount(/a/b/*) 3 3count(//*) 6 6count(//b) 2 2contains('base','as') true truecontains('base','x') false falsenot(contains('base','as')) false falsestarts-with('blabla','bla') true truestarts-with('blebla','bla') false falsesubstring('12345',2,3) 234 234substring('12345',2) 2345 2345substring('12345',2,6) 2345 2345concat('[',normalize-space(' before and after '),']') [before and after] [before and after]//* 6 62+3+4+5 14 1420-2-3+5 20 20count(/a/x[1]) 1 1name(/a/*[2]) x xname(/a/*[1]) b bname(/a/x[1]) x xcount(/a/b/c[1]) 1 1count(/a/b/c[position()=1]) 1 1count(/a/b/d[position()=3]) 0 0//*[@val]/@val 123 123//x[text()='sub text']/@target xyz xyzsubstring('123.4',1) 123.4 123.4//** syntax error syntax errorInput XML tree
<xml><text>
within </text>
</xml>
Expression | Result | Expected (compiled) |
---|---|---|
/xml/text/text() | within | within |
RSS feed examples
These examples show how to decode a typical XML file : the TinyXPath RSS feedRSS version (XPath expr : /rss/@version) : 2.0
Nb of news messages (XPath expr : count(/rss/channel/item)) : 7
Xpath expr | value |
---|---|
concat(/rss/channel/item[1]/pubDate/text(),' : ',/rss/channel/item[1]/title/text()) | Sun, 18 Jan 2004 13:33:55 GMT : TinyXPath 1.2.1 is released |
concat(/rss/channel/item[2]/pubDate/text(),' : ',/rss/channel/item[2]/title/text()) | Fri, 09 Jan 2004 21:24:09 GMT : TinyXPath 1.2 is released |
concat(/rss/channel/item[3]/pubDate/text(),' : ',/rss/channel/item[3]/title/text()) | Wed, 07 Jan 2004 08:52:10 GMT : Version 1.1 is out |
concat(/rss/channel/item[4]/pubDate/text(),' : ',/rss/channel/item[4]/title/text()) | Tue, 11 Jun 2002 19:31:51 GMT : Release 0.3 is out |
concat(/rss/channel/item[5]/pubDate/text(),' : ',/rss/channel/item[5]/title/text()) | Mon, 27 May 2002 20:14:31 GMT : Release 0.2 is out |
concat(/rss/channel/item[6]/pubDate/text(),' : ',/rss/channel/item[6]/title/text()) | Tue, 21 May 2002 21:23:53 GMT : First release 0.1 is out |
concat(/rss/channel/item[7]/pubDate/text(),' : ',/rss/channel/item[7]/title/text()) | Tue, 14 May 2002 20:09:56 GMT : Syntax decoding is done |