XPath examples
Output of xpath-examples.php:
-----------
XPath is like a file system
/books/book/title
-----------
<title>Apache Web-Server</title>
--
<title>Linux für Internet und Intranet</title>
--
-----------
// matches all nodes, which fullfill the following criteria
//title
-----------
<title>Apache Web-Server</title>
--
<title>Linux für Internet und Intranet</title>
--
-----------
The star * selects all nodes on that level
/books/book/*
-----------
<title>Apache Web-Server</title>
--
<year>2000</year>
--
<dc:subject>Webserver</dc:subject>
--
<title>Linux für Internet und Intranet</title>
--
<year>2000</year>
--
<dc:subject>Operating Systems</dc:subject>
--
-----------
Square brackets can further specifiy the nodes
/books/book[1]
-----------
<book id="1" isbn="3-8266-0612-4">
<title>Apache Web-Server</title>
<year>2000</year>
<dc:subject>Webserver</dc:subject>
</book>
--
-----------
The last book
/books/book[last()]
-----------
<book id="2" isbn="3-8266-0550-0">
<title>Linux für Internet und Intranet</title>
<year>2000</year>
<dc:subject>Operating Systems</dc:subject>
</book>
--
-----------
Attributes are prefixed by a @
/books/book/@isbn
-----------
isbn="3-8266-0612-4"
--
isbn="3-8266-0550-0"
--
-----------
Values of attributes can be used as selection criteria.
/books/book[@id = 2]
-----------
<book id="2" isbn="3-8266-0550-0">
<title>Linux für Internet und Intranet</title>
<year>2000</year>
<dc:subject>Operating Systems</dc:subject>
</book>
--
-----------
with count() the resulting nodes can be counted
(does not work with query())
count(//books)
-----------
-----------
name() returns the node name
//*[name() = 'title']
-----------
<title>Apache Web-Server</title>
--
<title>Linux für Internet und Intranet</title>
--
-----------
Several paths can be combined with | separator.
//title | //year
-----------
<title>Apache Web-Server</title>
--
<year>2000</year>
--
<title>Linux für Internet und Intranet</title>
--
<year>2000</year>
--
-----------
The descendant axis contains the descendants of the context node
/books/descendant::*
-----------
<book id="1" isbn="3-8266-0612-4">
<title>Apache Web-Server</title>
<year>2000</year>
<dc:subject>Webserver</dc:subject>
</book>
--
<title>Apache Web-Server</title>
--
<year>2000</year>
--
<dc:subject>Webserver</dc:subject>
--
<book id="2" isbn="3-8266-0550-0">
<title>Linux für Internet und Intranet</title>
<year>2000</year>
<dc:subject>Operating Systems</dc:subject>
</book>
--
<title>Linux für Internet und Intranet</title>
--
<year>2000</year>
--
<dc:subject>Operating Systems</dc:subject>
--
-----------
The parent axis contains the parent of the context node
/books/book[position() = 1]/title/parent::*
-----------
<book id="1" isbn="3-8266-0612-4">
<title>Apache Web-Server</title>
<year>2000</year>
<dc:subject>Webserver</dc:subject>
</book>
--
-----------
The following-sibling axis contains all the following siblings of the context node.
/books/book[position() = 1]/title/following-sibling::*
-----------
<year>2000</year>
--
<dc:subject>Webserver</dc:subject>
--
-----------
contains() checks, if a node contains the needle
//book[contains(dc:subject,"System")]
-----------
<book id="2" isbn="3-8266-0550-0">
<title>Linux für Internet und Intranet</title>
<year>2000</year>
<dc:subject>Operating Systems</dc:subject>
</book>
--
-----------
starts-with() checks, if a node starts with the needle
//book[contains(dc:subject,"Operating")]
-----------
<book id="2" isbn="3-8266-0550-0">
<title>Linux für Internet und Intranet</title>
<year>2000</year>
<dc:subject>Operating Systems</dc:subject>
</book>
--
XPath Summary
© copyright 2004 Bitflux GmbH