XPath visually

XPath visually

0 18360

XPath visually

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">
      <award>Trenton Literary Review Honorable Mention</award>
  <book style="textbook">
      <publication>Selected Short Stories of
  <magazine style="glossy" frequency="monthly">
    <subscription price="24" per="year"/>
  <book style="novel" id="myfave">
      <degree from="Trenton U">B.A.</degree>
      <degree from="Harvard">Ph.D.</degree>
      <publication>Still in Trenton</publication>
      <publication>Trenton Forever</publication>
    <price intl="Canada" exchange="0.7">6.50</price>
      <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>

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:

<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

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!



Leave a Reply