XPath visually

Tutorials

XPath is one of important keys to understanding of XSLT. And at first time we usually looking for any information (better – places where we can play online) – how to get different values in branch of XML tree. In this article I`ll give you all important information and will show how to work with XPath in samples.

As example we have next source XML:

01 <?xml version="1.0"?>
02 <bookstore specialty="novel">
03   <book style="autobiography">
04     <author>
05       <first-name>Joe</first-name>
06       <last-name>Bob</last-name>
07       <award>Trenton Literary Review Honorable Mention</award>
08     </author>
09     <price>12</price>
10   </book>
11   <book style="textbook">
12     <author>
13       <first-name>Mary</first-name>
14       <last-name>Bob</last-name>
15       <publication>Selected Short Stories of
16         <first-name>Mary</first-name>
17         <last-name>Bob</last-name>
18       </publication>
19     </author>
20     <price>55</price>
21   </book>
22   <magazine style="glossy" frequency="monthly">
23     <price>2.50</price>
24     <subscription price="24" per="year"/>
25   </magazine>
26   <book style="novel" id="myfave">
27     <author>
28       <first-name>Toni</first-name>
29       <last-name>Bob</last-name>
30       <degree from="Trenton U">B.A.</degree>
31       <degree from="Harvard">Ph.D.</degree>
32       <award>Pulitzer</award>
33       <publication>Still in Trenton</publication>
34       <publication>Trenton Forever</publication>
35     </author>
36     <price intl="Canada" exchange="0.7">6.50</price>
37     <excerpt>
38       <p>It was a dark and stormy night.</p>
39       <p>But then all nights in Trenton seem dark and
40       stormy to someone who has gone through what
41       <emph>I</emph> have.</p>
42       <definition-list>
43         <term>Trenton</term>
44         <definition>misery</definition>
45       </definition-list>
46     </excerpt>
47   </book>
48 </bookstore>

This is some bookstore listing

Here are list of rules which we can use in XPath:

  • Different levels in tree separate via / symbol. This example will return array with infos of all authors: bookstore/book/author
  • Access to attributes we can obtain via @ symbol. That example will return us price of magazine subscription: bookstore/magazine/subscription/@price
  • Inside [] we can use number value to tell which (by order) element will need to receive. This example will return us price of second book: bookstore/book[2]/price
  • Also, we can use subpaths in [] and use this as check, are necessary elements present in our tree or not. Example – get all books with style=novel: bookstore/book[@style=”novel”]
  • Another example – get all books with excerpts: bookstore/book[excerpt]
  • Another example – get all books if author have any awards: bookstore/book[author[award]]
  • Or – get all authors who have any awards: bookstore/book/author[award]
  • Or we can add some logic – get all books if author Don`t have awards: bookstore/book[author[not(award)]]
  • And last sample – get all books with price from 10 and 20: bookstore/book[price < 20 and price > 10]

Interesting, isn`t it? Now, how we can use that. In first sample – we will store some defined value into variable:

1 <xsl:variable name="magSubPrice" select="bookstore/magazine/subscription/@price"/>

Now we will walk through array of located elements:

1 <xsl:for-each select="bookstore/book/author">
2     // do some actions with authors
3 </xsl:for-each>

And, here are one interesting service: http://xpath.me/ – where you will able to play with all our samples online. It will allow you to feel all this by self. Very nice and easy service.


Conclusion

I hope that today’s article will very useful for your projects. Will glad if this will help you in your work. Good luck!

Rate article