Saturday, 16 July 2011

Example of an Absolute Location Path

XPath Location Path - Absolute

As described in the previous lesson, a location path specifies the path through the XML document's hierarchy that you'd like to work with.

Your location path can be absolute or relative. If your location path starts with the root node or a forward slash (/) you are using an absolute location path - your location path begins from the root node.

If your location path begins with the name of a descendant, you're using a relative location path. This node is referred to as the context node.

We'll look at relative location paths in the next lesson. For now, let's look at absolute location paths.

Example of an Absolute Location Path

Consider the following XML document:

 <albums>   <rock>     <title>Machine Head</title>     <artist>Deep Purple</artist>   </rock>   <blues>     <title>Greens From The Garden</title>     <artist>Cory Harris</artist>   </blues>   <country>     <title>The Ranch</title>     <artist>The Ranch</artist>   </country> </albums> 

If we wanted to select the "title" node of all albums, we could use the following (absolute) location paths:

 albums/rock/title albums/blues/title albums/country/title 

The Result

Here are the nodes that are selected using the above location path.

 <albums>   <rock>     <title>Machine Head</title>     <artist>Deep Purple</artist>   </rock>   <blues>     <title>Greens From The Garden</title>     <artist>Cory Harris</artist>   </blues>   <country>     <title>The Ranch</title>     <artist>The Ranch</artist>   </country> </albums> 
The Root Node

If we wanted to select the root node, we could use either the node's name or a forward slash. Both of these options are absolute location paths and select the root node.

Option 1 - use the root node's name:

 albums 

Option 2 - use a forward slash:

 / 

No comments:

Post a Comment