Browse DevX
Sign up for e-mail newsletters from DevX


Getting the Most Out of XML with XPath : Page 3

If you struggle sometimes with XML's complex tree structure and syntax, you'll want to get your hands on (and your head around) XPath. XPath makes it as easy to view and query XML data as it is to work with a basic file structure. Find out what this simple tool can do for you.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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

<books> <book> <name>Code Generation in Action</name> </book> <book> <name>Generative Programming</name> </book> </books>

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 XmlNodeList nodeList = doc.DocumentElement.SelectNodes("/books/book/name") Dim name As XmlNode For 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 XmlNode node = 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.

<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" /> <xsl:template match="/"> <xsl:for-each select="/books/book/name"> <xsl:value-of select="."/><xsl:text> </xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet>

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}\n" }

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}\n"

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.

Jack Herrington is the author of Code Generation in Action (Manning) and the editor of the Code Generation Network.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date