Complex XPath axes Methods
1. Parent
2. Child
3. Ancestor
4. Descendant
5. Following
6. Following-Sibling
7. Preceding
8. Preceding-Sibling
We'll use a sample XML structure related to vehicles to explore various XPath axes.
Sample XML File
<garage> <vehicle> <make>Toyota</make> <model>Camry</model> <year>2022</year> </vehicle> <vehicle> <make>Honda</make> <model>Civic</model> <year>2023</year> </vehicle> <vehicle> <make>Ford</make> <model>Mustang</model> <year>2021</year> </vehicle> </garage>
--------------------------------------------------------------------
1. Parent Axis
The parent axis selects the parent of the current node.
Example 1: Basic
XPath: parent::vehicle
Explanation: If the current node is <model>, the XPath parent::vehicle selects the <vehicle> element that contains this <model>.
Example 2: Intermediate
XPath: parent::*
Explanation: If the current node is <year>, the XPath parent::* selects the parent <vehicle> element. This selects any parent node, not just <vehicle>.
Example 3: Advanced
XPath: parent::garage
Explanation: If the current node is <make> inside any <vehicle>, the XPath parent::garage selects the <garage> element. This is useful when navigating up to the root element.
2. Child Axis
The child axis selects all children of the current node.
Example 1: Basic
XPath: child::vehicle
Explanation: From <garage>, the XPath child::vehicle selects all <vehicle> elements.
Example 2: Intermediate
XPath: child::make
Explanation: From <vehicle>, the XPath child::make selects the <make> element.
Example 3: Advanced
XPath: child::*
Explanation: From <vehicle>, the XPath child::* selects all child elements of <vehicle>, including <make>, <model>, and <year>.
3. Ancestor Axis
The ancestor axis selects all ancestors (parent, grandparent, etc.) of the current node.
Example 1: Basic
XPath: ancestor::*
Explanation: If the current node is <year>, the XPath ancestor::* selects the <vehicle> and <garage> elements.
Example 2: Intermediate
XPath: ancestor::vehicle
Explanation: If the current node is <model> inside <vehicle>, the XPath ancestor::vehicle selects the current <vehicle> element.
Example 3: Advanced
XPath: ancestor::garage
Explanation: If the current node is <model> inside a <vehicle>, the XPath ancestor::garage selects the <garage> element.
4. Descendant Axis
The descendant axis selects all descendants (children, grandchildren, etc.) of the current node.
Example 1: Basic
XPath: descendant::*
Explanation: From <garage>, the XPath descendant::* selects all elements: <vehicle>, <make>, <model>, and <year>.
Example 2: Intermediate
XPath: descendant::make
Explanation: From <garage>, the XPath descendant::make selects all <make> elements within <vehicle>.
Example 3: Advanced
XPath: descendant::year
Explanation: From <vehicle>, the XPath descendant::year selects the <year> element within that <vehicle>.
5. Following Axis
The following axis selects everything in the document after the closing tag of the current node.
Example 1: Basic
XPath: following::*
Explanation: If the current node is the first <vehicle>, the XPath following::* selects the second <vehicle> and the third <vehicle>.
Example 2: Intermediate
XPath: following::*
Explanation: If the current node is <make> of the first <vehicle>, the XPath following::* selects the <model> and <year> of the first <vehicle>, and all <vehicle> elements after it.
Example 3: Advanced
XPath: following::*
Explanation: If the current node is <year> of the first <vehicle>, the XPath following::* selects the <make>, <model>, and <year> of the remaining <vehicle> elements.
6. Following-Sibling Axis
The following-sibling axis selects all siblings after the current node.
Example 1: Basic
XPath: following-sibling::*
Explanation: If the current node is the first <vehicle>, the XPath following-sibling::* selects the second <vehicle> and the third <vehicle>.
Example 2: Intermediate
XPath: following-sibling::*
Explanation: If the current node is <make> of the first <vehicle>, the XPath following-sibling::* selects the <model> and <year> of the first <vehicle> and the next <vehicle> elements.
Example 3: Advanced
XPath: following-sibling::*
Explanation: If the current node is <year> of the first <vehicle>, the XPath following-sibling::* selects the <make>, <model>, and <year> of the second and third <vehicle> elements.
7. Preceding Axis
The preceding axis selects all nodes that appear before the current node in the document.
Example 1: Basic
XPath: preceding::*
Explanation: If the current node is the second <vehicle>, the XPath preceding::* selects the first <vehicle> and its <make>, <model>, and <year>.
Example 2: Intermediate
XPath: preceding::*
Explanation: If the current node is <make> of the second <vehicle>, the XPath preceding::* selects all nodes before it, including the first <vehicle> and its child elements.
Example 3: Advanced
XPath: preceding::*
Explanation: If the current node is <year> of the second <vehicle>, the XPath preceding::* selects all nodes before it, including the first <vehicle> and its children.
8. Preceding-Sibling Axis
The preceding-sibling axis selects all siblings before the current node.
Example 1: Basic
XPath: preceding-sibling::*
Explanation: If the current node is the second <vehicle>, the XPath preceding-sibling::* selects the first <vehicle>.
Example 2: Intermediate
XPath: preceding-sibling::*
Explanation: If the current node is <year> of the second <vehicle>, the XPath preceding-sibling::* selects the <model> of the second <vehicle> and all siblings before it.
Example 3: Advanced
XPath: preceding-sibling::*
Explanation: If the current node is <make> of the second <vehicle>, the XPath preceding-sibling::* selects the <make> of the first <vehicle>.
Combining All Categories: 10 XPath Queries
XPath:
parent::vehicle/child::model- Explanation: From
<make>, select the sibling<model>within the same<vehicle>.
- Explanation: From
XPath:
descendant::vehicle/ancestor::garage- Explanation: From any
<vehicle>, select the<garage>element.
- Explanation: From any
XPath:
following-sibling::vehicle/descendant::year- Explanation: From the first
<vehicle>, select the<year>of all following<vehicle>elements.
- Explanation: From the first
XPath:
preceding-sibling::vehicle/child::make- Explanation: From the second
<vehicle>, select the<make>of the preceding sibling<vehicle>.
- Explanation: From the second
XPath:
following::*[preceding-sibling::vehicle]/descendant::make- Explanation: From the first
<vehicle>, select the<make>elements of all following<vehicle>elements.
- Explanation: From the first
XPath:
ancestor::garage/descendant::vehicle/child::model- Explanation: From
<make>inside<vehicle>, select all<model>elements of<vehicle>descendants within<garage>.
- Explanation: From
XPath:
preceding::vehicle/following-sibling::vehicle/descendant::model- Explanation: From the third
<vehicle>, select the<model>elements of preceding<vehicle>siblings and all following<vehicle>siblings.
- Explanation: From the third
XPath:
descendant::make[following-sibling::vehicle]/ancestor::garage- Explanation: From
<make>elements that have following<vehicle>siblings, select the<garage>ancestor.
- Explanation: From
XPath:
following::vehicle[preceding-sibling::vehicle]/descendant::year- Explanation: From
<make>of any<vehicle>, select the<year>of all following<vehicle>elements with preceding siblings.
- Explanation: From
XPath:
preceding::vehicle/child::*[following-sibling::vehicle]- Explanation: From the second
<vehicle>, select all children that are siblings of any following<vehicle>elements.
- Explanation: From the second
0 Comments