4️⃣ XPath Tutorial – Master Navigating and Querying XML with XPath
Introduction – Why Learn XPath?
XPath (XML Path Language) is a query language used to navigate through elements and attributes in an XML document. It plays a vital role in technologies like XSLT, XQuery, and XML DOM, helping extract and manipulate specific XML data efficiently.
In this guide, you’ll learn:
- What XPath is and how it works
- XPath syntax and expression types
- Node selection techniques and axes
- Operators used in XPath filters
- Real-world use cases and examples
Topics Covered
| Topic | Description |
|---|---|
| XPath Introduction | Overview of XPath and its significance |
| XPath Nodes | Node types used in XPath navigation |
| XPath Syntax | Path expressions and filters explained |
| XPath Axes | Axes to traverse node relationships |
| XPath Operators | Logical and comparison operators |
| XPath Examples | Practical usage with real XML snippets |
XPath Introduction
XPath stands for XML Path Language. It is used to locate and process nodes in XML documents using a path expression.
XPath is platform-independent and works with:
- XSLT (transforming XML)
- XML DOM (programmatic access)
- XQuery (advanced XML queries)
XPath Nodes
XPath works with the XML DOM tree, which consists of various node types:
| Node Type | Description |
|---|---|
| Element | XML tags like <book> |
| Attribute | Attributes like id="123" |
| Text | Text content inside elements |
| Namespace | Used for XML namespaces |
| Comment | XML comments (<!--...-->) |
| Processing Instruction | Special instructions for XML parsers |
XPath Syntax
XPath expressions follow a file-system-like syntax:
➤ Basic Path Expressions
| Syntax | Description |
|---|---|
/ | Root node |
// | Anywhere in the document |
. | Current node |
.. | Parent node |
@ | Selects attributes |
➤ Examples:
/bookstore/book → Selects all book elements under bookstore
//title → Selects all title elements
/bookstore/book[1] → First book element
//@category → All category attributes
XPath Axes
Axes define node relationships in an XML tree.
| Axis | Description |
|---|---|
child:: | Direct children of current node |
parent:: | The parent of current node |
descendant:: | All descendants (children + deeper) |
ancestor:: | All ancestors (parents, grandparents) |
following-sibling:: | Siblings after current node |
preceding-sibling:: | Siblings before current node |
attribute:: | All attributes of the current node |
self:: | The node itself |
Example: book/child::title → selects the title child of a book
XPath Operators
XPath supports logical and comparison operators for complex queries:
| Operator | Description | Example |
|---|---|---|
= | Equal | price=30 |
!= | Not equal | @type!='fiction' |
<, > | Less than / greater than | price>20 |
and | Logical AND | price>10 and price<50 |
or | Logical OR | @type='fiction' or @type='tech' |
mod | Modulo | position() mod 2 = 1 |
XPath Examples
Given the following XML:
<library>
<book category="fiction">
<title>Book A</title>
<price>30</price>
</book>
<book category="tech">
<title>Book B</title>
<price>50</price>
</book>
</library>
Sample Queries
| XPath Expression | Result |
|---|---|
/library/book/title | All book titles |
//book[@category='tech'] | Book with category “tech” |
//book[price>40]/title | Titles where price > 40 |
//book[1]/title | Title of the first book |
//book[last()] | Last book node |
//@category | All category attributes |
Summary – Recap & Next Steps
XPath provides a concise and flexible way to query and navigate XML data structures. Its path expressions, axes, and logical operators make it indispensable in XML processing tasks.
Key Takeaways:
- XPath works with XML as a tree of nodes
- You can filter elements using syntax like
/,//,@, and[ ] - Axes define complex node relationships (parent, child, ancestor, etc.)
- Logical operators add filtering power to your queries
Real-World Relevance:
XPath is vital in web scraping, XML validation, config parsing, SOAP APIs, XSLT transforms, and more. It’s widely supported in browsers, backend languages, and developer tools.
FAQ – XPath
What is XPath used for?
XPath is used to query and select specific parts of an XML document, such as elements, attributes, and text.
Can XPath be used in JavaScript?
Yes, with evaluate() method in the browser or libraries like xmldom and xpath.js.
What’s the difference between / and // in XPath?
/ starts from the root; // searches anywhere in the document.
What are XPath axes?
Axes are ways to traverse node relationships like parent, child, ancestor, and sibling.
How does XPath differ from CSS selectors?
XPath works with structured XML and supports node types, whereas CSS selectors focus on HTML and style-oriented elements.
Share Now :
