XML — XPath Reference
XPath Syntax
| Expression | Description |
|---|---|
| nodename | All nodes |
| / | Root |
| // | Any position |
| . | Current |
| .. | Parent |
| @ | Attribute |
| * | Any element |
| @* | Any attribute |
Predicates
| Predicate | Description |
|---|---|
| [1] | First element |
| [last()] | Last element |
| [last()-1] | Second to last |
| [position()<3] | First two |
| [@attr='val'] | By attribute |
| [text()='val'] | By text |
| [element='val'] | By child |
Axes
| Axis | Description |
|---|---|
| ancestor | All ancestors |
| ancestor-or-self | Ancestors + self |
| attribute | Attributes |
| child | Children |
| descendant | All descendants |
| descendant-or-self | Descendants + self |
| following | Following nodes |
| following-sibling | Following siblings |
| namespace | Namespaces |
| parent | Parent |
| preceding | Preceding nodes |
| preceding-sibling | Preceding siblings |
| self | Current node |
Functions
| Function | Description |
|---|---|
| count() | Count nodes |
| sum() | Sum values |
| string-length() | String length |
| contains() | Contains text |
| starts-with() | Starts with |
| substring() | Substring |
| normalize-space() | Trim whitespace |
| translate() | Translate chars |
Mini Practice
- Write XPath with predicates
- Use different axes
- Apply functions
- Combine expressions
Up Next
Continue with Entity Reference — XML entity reference.
Related Topics
Frequently Asked Questions about XPath Reference
What is XPath Reference in XML?
XPath Reference is a fundamental concept in XML. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn XPath Reference?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn XPath Reference.
Why is XPath Reference important in XML?
XPath Reference is essential for XML development. Understanding this concept will help you write better code and solve real-world problems more effectively.