The (X)Path to Happiness

XML has steadily maintained its grip as one of the main standards for transmission of data. Depending on the programming language used, various options on how to handle XML files exist. However, XPath provides the ability to normalize how we handle XML data across various platforms.

Let’s say we cataloged our pets in an XML file, and it looked like something below:

  • <pets>
  • <cat>
  • <name>Whiskers</name>
  • <color>orange</color>
  • <food>
  • <favorite>tuna fish</favorite>
  • <regular>meow mix</regular>
  • <snack>mice</snack>
  • <drink>milk</drink>
  • <drink>water</drink>
  • </food>
  • <sound>meow</sound>
  • <toys>
  • <toy>catnip</toy>
  • .
  • .
  • .
  • </cat>
  • <tarantula>
  • .
  • .
  • </pets>

The problem we run into is, once our pets are all cataloged, we end up with a 5,000+ line XML document that is cumbersome to navigate manually. We didn’t realize that 25 cats could take up so much space in an XML file (and that we really need to get a social life). However, our work is not in vain! Sparky, one of our many dogs, is upset. Clearly, he wants his favorite food. Luckily, we made this XML file documenting all of our pets’ favorite foods. Now we just need to find that <dog> entry for Sparky.

Oh where, oh where has my little dog gone?

Unfortunately, we’re not familiar with the only programming language we have immediate access to, and thus don’t know how to handle XML files neatly inside of that language. Sparky is getting really upset, and we don’t want to waste time looking up how to do this in whatever language we are working on. This is where XPath comes to the rescue.

To use XPath, you execute a query on an XML object. This can be the document itself, or any individual node in the XML document. For these examples, we’ll assume we’re executing from the document itself. To find Sparky, we’ll try:

/dog

Pretty simple, right? This XPath query would return every node named ‘dog’ in the root node. The / signifies to search within the root node, which in our case is <pets>. However, this will return all our dogs, and we want a more direct answer.

/dog[name='Sparky']

Now we have our dog named Sparky! Oh… wait. Looks like we have multiple dogs named “Sparky”. Sorry, I’m not very original when naming my pets. Well, we can look at the Sparky thats upset and clearly wants his favorite treat, and see that his ears are short and his tail is long. Let’s query that:

/dog[name='Sparky' and ears='short' and tail='long']

Yeah! We found the right Sparky! We can expand our conditions using ‘and’ or ‘or’, but be careful: these keywords are case sensitive. Now that we have our right dog, we notice there are a lot of elements in here. Let’s make one final touch:

/dog[name='Sparky' and ears='short' and tail='long']/food/favorite

Tada! We found out that Sparky wants…. a bone. Wow. Didn’t see that coming. Well, at least we learned a little about XPath. To find out more, check out W3C’s documentation. XPath has a ton more functionality, including various functions to further pinpoint what you’re trying to find in a given XML document.

Michael Marr
About Michael Marr
Michael Marr is a staff writer for WebProNews

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>