X-path for beginners
December 30th, 2005 by Sonja DuijvesteijnXpath is used to get to a specific element, or node set in xml. But how does it work?
First, a piece of xml.
<?xml version="1.0" encoding="ISO-8859-1"?>
<navigation>
<menu>
<name>Contact</name>
<item id='1'>
<caption>Address</caption>
<link>http://link/</link>
</item>
<item>
<caption>Contact form</caption>
<link>http://link/</link>
</item>
<item>
<caption>Route description</caption>
<link>http://link/</link>
</item>
</menu>
</navigation>
This piece of xml describes a menu for a website, and the submenu that goes with it.
Now it’s very concievable you’ll first want to show the name of the menu.
/navigation/menu/name
This is much like a directory structure. The / means it’s starting from the root node (navigation), from there it goes to menu, and on to the name. If there were more menu nodes in the xml this would not only point to the name node with the contents “contact” but also to all other nodes “name” in “item” in “navigation”.
Now, as the previous example shows you can get to the node name and similarly get to the (submenu) items in menu. But what if you want to specifically get the first item?
/navigtion/menu/item[1]
/navigation/menu/item[position()= 1]
/navigation/menu/item[@id="1"]
All of these do the same thing. They select the first node with the name “item” for all menu nodes in navigation. By using the [] you can, as with arrays in most languages, get the node you want. But starting with 1, as there is no zeroth node.
The second example shows basically the same thing, except in this case the position of the node is tested wether it’s one. Position() is one of the functions that you can use in Xpath.
The last one shows a test wether the attribute “id” is in the node and it’s value is 1. Of course
these tests can also be done for menu. Getting the last item in the first menu could be accessed like this.
/navigation/menu[position() = first()]/item[position() = last()]
First() and last() are two more functions from xPath, their use is simple, they select the first and last node of the nodeset.
Another part of XPath are axis. A node in an xml tree can be seen as part of a family. Each node can have children, siblings, ancestors and such. You don’t always start from the root node in a document but can start from another node as well. When you’re in the last item node and you want to get the name of menu you’d use the following syntax:
parent::node()/name
This selects the parent of item, which is a node and from there to the name of the menu.
With these simple rules you can access all nodes. Of course there’s more to it, for a good explanation check out w3schools.
Related posts
Reasons to use CSS instead of tables
HTML and XHTML, which to use?
September 30th, 2009 at 3:55
Greetings all…
So after nearly 5 years of focusing on the finance sector and really enjoying every minute of it…the time has come to expand my horizons and take the next step in delivering great products for Microsoft. So as of very soon (date TBD) I will be moving to a Program Management role in a new product being developed in the Office division.