XPath Parent

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

XPath - Parent ..

This lesson will teach you how to select the parent of an element in your XPath expressions.

XML Code, lemonade2.xml:

		<lemonade supplier="mother" id="1">
		<pop supplier="store" id="2">

		<chips supplier="store" id="3">


XPath - Selecting a Parent with ..

Normally in an XPath expression, you have a string of elements separated by a slash. However, if you put two periods ".." where an element would normally go you can select the parent element. The element to the left of the double period will have its parent selected.

For example, if we wanted to select all of the product elements, lemonade, pop, and chips we would have to do a few expressions.

Normal XPath Expression:


However, we could instead select the parent element of amount to get the same three product elements. This is because lemonade, pop and chips all have a child amount.

Parent XPath Expression:


This expression uses a relative path location and selects all of the amount elements in our XML document. It then uses the parent sequence ".." to select the parent element of each amount element. In our XML document, there are three amount elements, and their parents are lemonade, pop and chips!