UNDERSTANDING XPATH

XPath, or XML Path Language, is one of those things that takes a while to grasp, at least for most developers. It's been compared to regular expressions, which in my opinion is a bit much because it's not nearly that complicated. XPath is a query language for selecting nodes from an XML document source. In the Umbraco scenario, you use XPath as part of the XSLT to target nodes from the umbraco.config (XML cache) mentioned earlier in this chapter. In a nutshell, XPath allows you to traverse any node structure by providing a set of steps that make up a path, or more complex instructions to programmatically manipulate the output from the source XML. To illustrate the concept, before you dig into the practicals of using XPath in Umbraco, take a look at Listing 11-1 as a basis for the following XPath examples. This is an abbreviation of the classic book.xml example that you see in most XSL/XPath examples on the Internet.

LISTING 11-1: books.xml

image
<?xml version=“1.0”?>
<catalog>
   <book id=“bk101”>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
   <book id=“bk102”>
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.</description>
   </book>
   <book id=“bk103”>
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology
      society in England, the young survivors lay the
      foundation for a new society.</description>
   </book>
</catalog>

The following example XPath statement queries the XML source for the book with an Id of bk102:

<xsl:variable name=“theSecondBook” select=“//catalog/book [@id='bk102']” />

This returns the entire book node and its children, as shown in the following code snippet.

<catalog>
   
   <book id=“bk102”>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
   
</catalog>

If you now wanted to get the title of the returned book, you could use the following:

<xsl:value-of select=“$theSecondBook/title” />

The example of selecting a single book is practical only if you know the Id of the book that you're looking for. Instead, you can output all the books in the inventory by performing a standard for-each loop. The following example returns all the nodes whose name equals book.

<xsl:for-each select=“//catalog/book”>
  <h3><xsl:value-of select=“current()/title” /></h3>
  <p><xsl:value-of select=“current()/description” /></p>
  <hr />
</xsl:for-each>

Again, these are simple cases but should provide you with a basic idea of how paths can ultimately get you to the information that you seek. All the paths in the code are based on the notion that you know the structure of the XML that you are querying. If you don't know that, then you don't know what to ask for. This, of course, is true in other environments as well, such as using SQL to query database tables.

In addition to simply writing out paths as they are formed in the XML document, you can use something called an XPath axis. Axes allow you to step up, down, and sideways within the XML structure to grab data. You use them by name (see Table 11-1 for available axes), along with a node test (that is, the node name that you are looking for) and one or more predicates. A predicate is defined as [consisting] “of an expression, called a predicate expression, enclosed in square brackets. A predicate serves to filter a sequence, retaining some items and discarding others.” The axis and node test are separated by ::, and the predicate/s are enclosed in [].

image Speaking of structures, if you need to look at the structure of the XML that you are querying—for example, if you didn't know what the structure of the <catalog /> was—you could output the contents of the catalog node by writing <xsl:copy-of select=“catalog” />. This lists the catalog node and all of its children, just like you see in Listing 11-1. Note that when viewing the results of an <xsl:copy-of /> in a browser, you must view the source to see the output because the XML tags won't display.

TABLE 11-1: XPath Axes

AXIS DESCRIPTION
ancestor Contains the ancestors of the context node. Ancestors include the parent, and its parent, and its parent, and so on, all the way back up to the root node.
ancestor-or-self Contains the context node and its ancestors.
attribute Contains the attributes of the context node.
child Contains the children of the context node.
descendant Contains the descendants of the context node. Descendants include the node's children, and that child's children, and its children, and so on (until there are no more children).
descendant-or-self Contains the context node and its descendants.
following Contains all nodes that come after the context node (that is, after its closing tag).
following-sibling Contains the following siblings of the context node. Siblings are at the same level as the context node and share its parent.
namespace Contains the namespace of the context node.
parent Contains the parent of the context node.
preceding Contains all nodes that come before the context node (that is, before its opening tag).
self Contains the context node.

See the examples in the end of this chapter for more details on how to use XPath within Umbraco.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.162.26