🧾 XPath Syntax – Learn the Language to Navigate XML
🧲 Introduction – Why Learn XPath Syntax?
XPath is a query language for selecting parts of an XML document. To use it effectively, you need to understand its syntax rules, expression types, and how to form valid queries. Whether you’re building XML extractors, filters, or processors, mastering XPath syntax is essential.
🎯 In this guide, you’ll learn:
- Basic and advanced XPath syntax
- How to write absolute and relative paths
- Use of wildcards, predicates, and functions
- Examples of expressions that target elements, attributes, and values
🧱 Core XPath Expression Structure
An XPath expression is similar to a file path, but targets XML elements and attributes.
/path/to/element[@attribute='value']
- Starts with
/
(absolute) or//
(relative) - Targets nodes with tag names
- Uses
@
to reference attributes - Can include
[predicate]
filters for values or positions
📄 Sample XML
<library>
<book id="101">
<title lang="en">XPath Basics</title>
<author>Jane Doe</author>
<price>499</price>
</book>
<book id="102">
<title lang="en">Advanced XML</title>
<author>John Smith</author>
<price>599</price>
</book>
</library>
🧭 XPath Path Syntax Overview
Syntax | Meaning |
---|---|
/ | Root of the document |
// | Anywhere in the document |
. | Current node |
.. | Parent node |
@ | Attribute selector |
[] | Predicate (filter by condition or position) |
📘 XPath Expression Examples
Expression | Result Description |
---|---|
/library/book | All <book> nodes directly under <library> |
//book | All <book> nodes anywhere in the document |
//title | All <title> nodes |
//book[@id='101'] | <book> node with id="101" |
//book[2] | The second <book> node |
//price[text() > 500] | Prices greater than 500 |
//title[@lang='en'] | English-language <title> nodes |
//book[author='Jane Doe'] | <book> with <author> = “Jane Doe” |
🔄 Absolute vs Relative XPath
Type | Syntax | Description |
---|---|---|
Absolute | /library/book[1] | From document root |
Relative | //book[1] | Anywhere in document |
✨ XPath Wildcards
Symbol | Meaning |
---|---|
* | Matches any element node |
@* | Matches any attribute node |
Examples:
//book/* → All children of `<book>`
/library/*/title → All `<title>` nodes under any child of `<library>`
📦 XPath Predicates ([]
)
Predicates filter nodes based on:
- Index/position
- Attribute values
- Node text
//book[1] → First `<book>`
/library/book[@id='101'] → Book with ID 101
//price[text() > 500] → Prices over 500
🧠 XPath Functions (Common Examples)
Function | Usage Example | Description |
---|---|---|
text() | //title/text() | Selects inner text |
contains() | //title[contains(text(),'XML')] | Partial text match |
starts-with() | //author[starts-with(text(),'J')] | Starts with condition |
position() | //book[position()=1] | Node position check |
last() | //book[last()] | Selects the last matching node |
✅ Best Practices for XPath Syntax
- ✔️ Always test XPath expressions with a validator or IDE
- ✔️ Use predicates to filter large datasets efficiently
- ✔️ Prefer
//
for flexibility and/
for strict hierarchy - ✔️ Use meaningful paths over generic wildcards (
*
) for accuracy - ❌ Avoid overly complex chains—break into manageable parts
📌 Summary – Recap & Next Steps
XPath syntax lets you expressively query and filter XML content. From basic element access to advanced filtering and pattern-matching, learning this syntax gives you full control over any XML document structure.
🔍 Key Takeaways:
- Use
/
and//
to define location - Use
@
to access attributes - Use
[]
for filtering by position or condition - Combine wildcards and functions for advanced selection
⚙️ Real-world relevance: XPath syntax powers XML parsing, document editing, testing tools, API data extractors, and more.
❓ FAQs – XPath Syntax
❓ What’s the difference between /
and //
in XPath?
✅ /
is for exact path from the root; //
is for searching anywhere in the document.
❓ How do I access an attribute in XPath?
✅ Use @attributeName
, like //book/@id
.
❓ What does *
mean in XPath?
✅ It selects any element. Use carefully to avoid overmatching.
❓ How do I select the last node?
✅ Use //book[last()]
.
❓ Can I combine conditions in XPath?
✅ Yes. Use and
, or
, and functions like contains()
inside []
.
Share Now :