XPath Absolute

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.

XPath Expression:


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: