Browse DevX
Sign up for e-mail newsletters from DevX


Getting the Most Out of XML with XPath

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

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:

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

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:


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.

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

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.

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