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:
<lemonade supplier="mother" id="1">
<pop supplier="store" id="2">
<chips supplier="store" id="3">
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.
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.
Download Tizag.com's XML Book
If you would rather download the PDF of this tutorial, check out our
XML eBook from the Tizag.com store.
Found Something Wrong in this Lesson?
Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!