All things XPath

What’s XPath?

So XPath is really just a means of accessing data on xml nodes. XML has structure, with node and branches and attributes; XPath is the notation we use to express which nodes, branches or attributes we want to access. XSL makes use of this notation in order to change the structure and layout of an XML document. The process of changing one form of xml into another is called an XSL Transformation, or xslt for short.

The Basics!

If you’ve ever laid out a web page, YOU ALREADY KNOW XPATH! More specifically, if you have ever used an image in a webpage, you already some understanding of how xpath works. Take the following example:

<img src=”/images/my_image.gif“/>

The value of the “src” attribute is an xpath expression. It means the following: select the image that resides in the folder named “images” at the root of the web server’s domain, and choose the image named “my_image.gif”. In this example, the server’s directory structure is the XML we are navigating, and the image location is the data we want to transform to reuse in a new location.

If you’ve ever used a command prompt or terminal, you’ll notice that xsl is a little more intuitive because of expressions like this:

../siblingElement = go up one directory and pick the “siblingElement”
./currentElement = pick node “currentElement” in the current location
/rootElement = pick the node named “rootElement” at the base of the xml structure
node/childElement = pick the node named “childElement” inside the node named “node” which is in the current location

In addition, there are ways of accessing node attributes with the “@” sign:

< blockquote >../siblingElement/@id = go up one directory and pick the attribute “id” belonging to the node named the “siblingElement”
./currentElement/@name = pick attribute named “name” belonging to the node “currentElement” in the current location
/rootElement/@rootId = pick the attribute named “rootId” belonging to the node named “rootElement” at the base of the xml structure
node/childElement /@count= pick the attribute named “count” belonging to the node named “childElement” inside the node named “node” which is in the current location

Conditional Selectivity

XPath allows you to use conditional logic to navigate your xml document. Consider the following xml:

<xml>
	<node name="first"/>
	<node name="second"/>
</xml>

What kind of expression would you use to say “pick the node with the attribute named ‘first’”? This kind of logic is not only possible, but aslo can allow for extremely complicated xpath expressions. This is how to do it:

/xml/node[@name = 'first']

This literally means – get all the nodes in the xml where the /xml/node ‘s attribute named “name” has the value “first”. It will return ALL xml nodes that satisfy that requirement. The reason xpath expressions can become so complicated by this ability is that poorly constructed xml will force people to cross link attribute references between this branch and that so a really wordy xpath expression needs to be used to used as the selector.

Tangent!

This is my terrible segue way into some comments on xml! when constructing xml, please structure it as simply as possible with the forethought that xml should be extensible, as concise as possible, and xpath friendly as you can build it. Structure is the most important thing your xml schema should express and some genuine forethought and planning there will really pay off in the long run.

Advanced XPath

XPath has a lot of other commands that can be used for selecting nodes. I’ll list some interesting constructs below:

Select the node name “poll” with the namespace “ns”
ns:poll

Select all attributes named “id” for all the elements named “example”
poll/@id

Select the first poll node
poll[1]

Select all poll elements whose id is “100″ and whose count is > 0:
example[@id = "100" and @count &gt; 0]
Notice it uses the html entity &amp;, this is because angle brackets break xml, specifically “>” breaks the current xml as the parser would not know it was text and not a “close node definition” instruction

Select the nearest poll ancestor of the current element:
ancestor::poll[1]
if you drop the [1] from the expression, it seems to choose the farthest node

An example of XPath unions:
pollVote | pollVote/vote
this means pick all nodes that satisfy either selector expression “pollvote” or “pollVote/vote”



Related posts:

  1. 5 ways to make XML more XPath friendly
  2. Transforming XML into MS Excel XML
  3. Java, XML and XStream

Comments (0)

› No comments yet.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.