Getting the Most Out of XML with XPath

ML has now taken over the representation and storage of structured data in a textual format. No longer do we even have a need for new text formats because XML and its related standards do such a good job, and are so ubiquitous, that new standards are no longer needed. Understanding and using XML effectively is a critical skill for software engineers.

But even with books on XML and handy software?such as validators (e.g. DTD, RelaxNG), editors (e.g. XMLSpy, oXygen), and translators (i.e. XSLT)?to help us use XML more efficiently, it can still be difficult. This comes down to two major factors, the first is that XML is verbose, which we can do little about, and the second is that XML is often represented in memory as a tree structure, and tree structures are notoriously difficult to navigate.

Thankfully, because XML is so structured you can use an XML-related standard called XPath to quickly locate any information you need in an XML tree.

The Basics of XPath
The ‘path’ in ‘Xpath’ gives us a clue as to how we use this mechanism to specify nodes in an XML tree. Let’s take a very simple piece of XML tree:

            Code Generation in Action    

If you think about the nodes in the tree as a directory it would look like this:

/  books      book         name

The absolute path to the ‘name’ information in a directory structure would be:

/books/book/name

It’s exactly the same in XPath. In fact, the XPath statement above will work properly in XML to return the title of the book:

Author’s Note: Throughout this article, yellow highlight is used to show what information from the original XML would be returned by the Xpath.
            Code Generation in Action    

Of course, there is a lot more to learn about XPath, but fundamentally you can think of an XPath in XML the same way you would think about a path in the operating system.

XPath Variations
One thing the observant reader will have noticed from the original example was that the first XPath would have returned more than one book name if there had been more than one book in the list. For example:

            Code Generation in Action                Generative Programming    

will return both the ‘Code Generation in Action’ and ‘Generative Programming’ name nodes from the XML when you use this XPath:

/books/book/name

The selected nodes are:

            Code Generation in Action                Generative Programming    

To refine the search to the first or second book you can provide ordinal values.

/books/book[1]/name

The statement above selects the first node:

            Code Generation in Action                Generative Programming    

And this will return the second:

/books/book[2]/name

So what does an XPath statement return? It returns either a list of nodes or a single node, where a node is either a tag element or an attribute. Most APIs have a method for fetching all of the nodes matching an XPath query as a list or returning the first match as a single node.Getting back to the previous example, every element and tag has a unique XPath address. You can think of a fully qualified path to the first book name as:

/books[1]/book[1]/name[1]

Handling Attributes
Tags aren’t the only things in XML trees though; you also need to be able to access attributes. Let’s say we had this XML input file:

        

To get the names of the books with this schema you need to add the ‘@’ operator into your use of the XPath syntax:

/books/book/@name

This XPath statement will give us the ‘name’ attribute nodes for each of the books:

    name=">Code Generation in Action" />    name=">Generative Programming" />

The easy way to remember to use the ‘@’ is just to think of it as ‘at,’ which is the start of the word ‘attribute.’ You can get a little more complex by using attributes instead of ordinals to refine your queries. For example, consider the following XML tree:

                              

To get all of the book tags from ‘Manning’ you can use this XPath:

/books/publisher[@name='Manning']/book

which returns:

                              

To get just the names of the Manning books, refine the XPath a bit more:

/books/publisher[@name='Manning']/book/@name

This returns a smaller node list:

           name="Code Generation in Action" />                   

XPath queries that use the bracket notation can be extremely complex, as they use Boolean logic and built-in functions to refine queries. A deep discussion of this syntax is beyond the scope of this article, but these complex queries can be very handy. To learn them, get a copy of Michael Kay’s excellent “XSLT Programmer’s Reference,” which discusses XPath at this level of depth.

Because XML is a tree you may find that the elements that you are interested are scattered about the tree at various levels. Take this XML:

                                               

In one case the books are grouped (in the tag “technical”) and in the other the books are not. No matter, XPath uses a wildcard syntax to make finding them easy. This XPath query will find all of the book nodes regardless of their location:

//book

Which results in this selection:

                                               

The following query will find just the names of the books at any level:

//book/@name

Sometimes you need to get all of the nodes within a particular structure. Take this XML tree:

                         

What happens when you want to get anything written by the author with the id of ‘herrington‘? In the operating system you would use ‘*’ and the same works in XPath:

/media/author[@id='herrington']/*

This statement returns the three nodes within the author tag:

                         

To get the name attributes, though, you need to make a slight modification:

/media/author[@id='herrington']/*/@name

And that creates a little tighter node set:

           name="Code Generation in Action" />       name="Code Generation Network" />       
name="PHP Scalability Myth" />

What I haven’t yet covered is the context for the query. So far all of the XPath we have used starts at the root of the tree. But what happens when you are deep in the file system and you want to just go back a step? In the operating system you can use ‘..’ to specify the previous node. The same works in XPath.When you run an XPath query you give it two things, the first is the starting node (most often the document root), and then the XPath query string.

                     

If you start with the book node in the example above, you can use the ‘..’ notation to go up one level to the author id tag and get the first and last names of the author from the first and last attributes:

../@first 

and:

../@last

Now that you know how handy XPath can be the next question is, how do you use it? Thankfully all of the major programming languages have XPath support built into their support for XML.

XPath Usage Example
My first example of XPath in use is implemented in VB.NET.

            Code Generation in Action                Generative Programming    
Figure 1. Spies Like Us: You can use XMLSpy by Altova Software to select XML nodes using XPath.

Given the XML above, the following VB.NET code would find all of the name nodes and put up a message box with the text of each node.

Dim nodeList As XmlNodeListnodeList = doc.DocumentElement.SelectNodes("/books/book/name")Dim name As XmlNodeFor Each name In nodeList MsgBox(name.InnerText)Next

You can use this code to find just the name of the first book:

Dim node As XmlNodenode = doc.DocumentElement.SelectSingleNode("/books/book/name")MsgBox(node.InnerText)

Similar recipes work for C#:

XmlNodeList nodes = doc.DocumentElement.SelectNodes( "/books/book/name" );foreach( XmlNode node in nodes ){    Console.WriteLine( node.InnerText );}

This code finds all of the name nodes and prints them to the console. Getting a single node looks like this:

XmlNode singleNode = doc.SelectSingleNode( "/books/book/name" );Console.WriteLine( singleNode.InnerText );

In XSLT you use XPath constantly to specify nodes or template-matching criteria.

        

This style sheet prints out each book name as text with line breaks between them. The ‘match’ attribute on the template is XPath and so are the ‘select’ attributes on the for-each and value-of tags.

Scripting languages, such as Ruby, make it very simple to use XPath. Here is how Ruby, through the REXML API, gets all of the name nodes:

doc.each_element( '/books/book/name' ) { |name| print "#{name.text}
" }
Figure 2. Book by Its Cover: Visual XPath makes it easy to experiment with XPath.

In this case you simply send your XPath string to the each_element method on the root node. To get a single node is just as easy:

elem = REXML::XPath.first( doc, '/books/book/name' )print "#{elem.text}
"

Java supports XPath as part of the base J2ME library. C++ can support XPath through Xalan, which sits on top of the Xerces XML parser. Whatever language you choose, if it supports XML it will probably support XPath.

XPath in Applications

Figure 3. XML in the Shell: XMLStartlet lets you do a simple a search on your XML code using a command line interface.

Not only is there support for XPath in programming languages, it’s also in canned applications. Altova’s XMLSpy is an editor for XML with support for searching on XPath built into the interface (see Figure 1).

If you aren’t in the mood to get an XMLSpy license you can still play with XPath interactively using VisualXPath, an open source XPath query analyzer for Windows (see Figure 2).

For Unix environments you can use XPath in a shell by installing XMLStartlet and running xmllint (see Figure 3).

Xmllint turns your XML into a file system and allows you to search it with XPath. It’s an easy way to take a tour around XML you don’t know.

XPath is a great language for two reasons. First, it’s easy to learn. And second, it’s incredibly useful. Learning and using XPath will make it so much easier to process XML code in your applications. It will also open up a gateway to technologies such as XSLT that are fundamental to XML and which make extensive use of XPath.

Author’s Note: In this article I talked about XPath 1.0. There are some tools that support the draft 2.0 spec, but overall it’s not yet well adopted.
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: