Introduction to XPath - Path Expression Syntax
(Page 2 of 4 )
The basic path expression syntax is similar in style to file-system addressing. If the path starts with the slash ( / ), then it represents an absolute path to the required element. Only the / represents the root element. For the example above, the expression "/" represents <bookstore>. To get all the <book> elements use:
/book
This will return both <book> elements. To get the <price> elements use:
/book/price
<bookstore>
<book>
<title id='1'>XPath Tutorial</title>
<author>Mamun Zaman</author>
<year>2007</year>
<price>00.99</price>
</book>
<book>
<title id='2'>AJAX Tutorial</title>
<author>Charles</author>
<year>2007</year>
<price>03.45</price>
</book>
</bookstore>
If the path starts with // then all elements in the document which fulfill the following criteria are selected. The //B expression selects all <B> tags regardless of their parents.
<root>
<B/>
<C/>
<B/>
<D>
<B/>
</D>
<C>
<D>
<B/>
<B/>
</D>
</C>
</root>
The star * selects all elements located by the preceding path. To select all elements under <book> use the /book/* path expression.
<bookstore>
<book>
<title id='1'>XPath Tutorial</title>
<author>Mamun Zaman</author>
<year>2007</year>
<price>00.99</price>
</book>
<book>
<title id='2'>AJAX Tutorial</title>
<author>Charles</author>
<year>2007</year>
<price>03.45</price>
</book>
</bookstore>
Next: Getting Elements >>
More XML Articles
More By Mamun Zaman