About Me

Friday, 13 April 2012

XPath - Absolute Location

XPath - Absolute Location

Up to this point, we have been selecting elements by giving the full path. We start with the the root element and end with the desired descendant element. This method of using the complete path is referred to as absolute location path, and it is useful for selecting a specific element.


There is another method, relative location path, which is useful when you want to select many similar items (more on this later). This lesson will provide an overview of absolute location paths in XPath.
We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - Start with the Root Element

When you are using the absolute method, your expression must start with the root element of your XML document (inventory in our example). If you only wanted to select the root element, then you're done.

XPath Expression:

inventory
However, if you wanted one of the sub-elements, like lemonade, then you will have to create a string of children elements from the root node to the desired node.

XPath Expression:

inventory/drink/lemonade

0 comments:

Post a Comment