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:
<?xml version="1.0"?> <bookstore specialty="novel"> <book style="autobiography"> <author> <first-name>Joe</first-name> <last-name>Bob</last-name> <award>Trenton Literary Review Honorable Mention</award> </author> <price>12</price> </book> <book style="textbook"> <author> <first-name>Mary</first-name> <last-name>Bob</last-name> <publication>Selected Short Stories of <first-name>Mary</first-name> <last-name>Bob</last-name> </publication> </author> <price>55</price> </book> <magazine style="glossy" frequency="monthly"> <price>2.50</price> <subscription price="24" per="year"/> </magazine> <book style="novel" id="myfave"> <author> <first-name>Toni</first-name> <last-name>Bob</last-name> <degree from="Trenton U">B.A.</degree> <degree from="Harvard">Ph.D.</degree> <award>Pulitzer</award> <publication>Still in Trenton</publication> <publication>Trenton Forever</publication> </author> <price intl="Canada" exchange="0.7">6.50</price> <excerpt> <p>It was a dark and stormy night.</p> <p>But then all nights in Trenton seem dark and stormy to someone who has gone through what <emph>I</emph> have.</p> <definition-list> <term>Trenton</term> <definition>misery</definition> </definition-list> </excerpt> </book> </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/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:
<xsl:variable name="magSubPrice" select="bookstore/magazine/subscription/@price"/>
Now we will walk through array of located elements:
<xsl:for-each select="bookstore/book/author"> // do some actions with authors </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.
I hope that today’s article will very useful for your projects. Will glad if this will help you in your work. Good luck!