XPath Examples

Welcome to The Coding College! In this tutorial, we’ll demonstrate practical XPath Examples for navigating and querying XML documents effectively. XPath, or XML Path Language, is an essential tool for extracting specific elements, attributes, or text from an XML file. Let’s dive in!

Example XML Document

We’ll use the following XML document as a reference for our examples:

<library>
  <book id="1" genre="Programming">
    <title>XML Basics</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book id="2" genre="Fiction">
    <title>The Great Adventure</title>
    <author>Jane Smith</author>
    <price>19.99</price>
  </book>
  <book id="3" genre="Programming">
    <title>Learn JavaScript</title>
    <author>Chris Lee</author>
    <price>39.99</price>
  </book>
</library>

XPath Query Examples

1. Select All Nodes

XPath Expression:

/*

Result:
Selects the root element:

<library>
  ...
</library>

2. Select All Child Nodes

XPath Expression:

/library/*

Result:
Selects all child <book> elements:

<book id="1" genre="Programming">
  ...
</book>
<book id="2" genre="Fiction">
  ...
</book>
<book id="3" genre="Programming">
  ...
</book>

3. Select Specific Element

XPath Expression:

/library/book[1]

Result:
Selects the first <book> element:

<book id="1" genre="Programming">
  <title>XML Basics</title>
  <author>John Doe</author>
  <price>29.99</price>
</book>

4. Filter by Attribute

XPath Expression:

/library/book[@genre="Programming"]

Result:
Selects all <book> elements with genre="Programming":

<book id="1" genre="Programming">
  ...
</book>
<book id="3" genre="Programming">
  ...
</book>

5. Select Elements Based on Text Content

XPath Expression:

/library/book[title="XML Basics"]

Result:
Selects the <book> with <title>XML Basics</title>:

<book id="1" genre="Programming">
  <title>XML Basics</title>
  <author>John Doe</author>
  <price>29.99</price>
</book>

6. Select Attribute Value

XPath Expression:

/library/book/@id

Result:
Selects the id attributes of all <book> elements:

1  
2  
3

7. Select Nodes Based on Position

a) First Node:

/library/book[position()=1]

b) Last Node:

/library/book[position()=last()]

Result for Last Node:

<book id="3" genre="Programming">
  <title>Learn JavaScript</title>
  <author>Chris Lee</author>
  <price>39.99</price>
</book>

c) All Except First Node:

/library/book[position() > 1]

8. Select All Descendants

XPath Expression:

/library/descendant::*

Result:
Selects all nodes under <library>:

<book id="1" genre="Programming">
  ...
</book>
<book id="2" genre="Fiction">
  ...
</book>
<book id="3" genre="Programming">
  ...
</book>
<title>XML Basics</title>
<title>The Great Adventure</title>
<title>Learn JavaScript</title>
<author>John Doe</author>
<author>Jane Smith</author>
<author>Chris Lee</author>
<price>29.99</price>
<price>19.99</price>
<price>39.99</price>

9. Filter Nodes by Logical Operators

a) AND Condition:

/library/book[@genre="Programming" and price < 40]

Result:
Selects <book> elements where genre="Programming" and price < 40:

<book id="1" genre="Programming">
  <title>XML Basics</title>
  <author>John Doe</author>
  <price>29.99</price>
</book>

b) OR Condition:

/library/book[@genre="Programming" or price < 20]

Result:
Selects <book> elements where genre="Programming" or price < 20:

<book id="1" genre="Programming">
  ...
</book>
<book id="2" genre="Fiction">
  ...
</book>
<book id="3" genre="Programming">
  ...
</book>

10. Select Parent Node

XPath Expression:

//price/parent::book

Result:
Selects the <book> element that is the parent of <price>:

<book id="1" genre="Programming">
  ...
</book>
<book id="2" genre="Fiction">
  ...
</book>
<book id="3" genre="Programming">
  ...
</book>

11. Select Based on Partial Attribute Value

XPath Expression:

/library/book[contains(@genre, "Prog")]

Result:
Selects <book> elements where the genre attribute contains “Prog”:

<book id="1" genre="Programming">
  ...
</book>
<book id="3" genre="Programming">
  ...
</book>

12. Count Elements

XPath Expression:

count(/library/book)

Result:
Counts the number of <book> elements:

3

13. Union of Nodes

XPath Expression:

//title | //author

Result:
Selects all <title> and <author> elements:

<title>XML Basics</title>
<title>The Great Adventure</title>
<title>Learn JavaScript</title>
<author>John Doe</author>
<author>Jane Smith</author>
<author>Chris Lee</author>

14. Select Nodes by Attribute Not Equal to Value

XPath Expression:

/library/book[@id != "2"]

Result:
Selects <book> elements where id is not “2”:

<book id="1" genre="Programming">
  ...
</book>
<book id="3" genre="Programming">
  ...
</book>

Conclusion

XPath expressions are versatile and powerful tools for navigating and querying XML documents. By mastering these examples, you’ll be well-equipped to handle any XML data extraction challenges.

Leave a Comment