XPath: Node Selection, Axes, Predicates, and Functions
Unit 5âĸCLO03
Learning Objectives
Course Learning Outcomes
CLO03
Course Outcomes
CO04
âšī¸
Introduction
XPath (XML Path Language) is the standard way to navigate an XML tree and select nodes. It is the query core for XSLT and many XML processing APIs. This topic covers path expressions, predicates, axes, and common functions used in exams.
The Basics
XPath essentials
- Absolute paths start with
/ - Relative paths start from the current context
- Predicates filter with brackets
Technical Details
Axes
Common axes:
- child:: (default)
- parent::
- ancestor::
- descendant::
- following-sibling::
Examples:
/library/book/title//book[@id='b2']/price/text()count(/library/book)
Examples
Example XML
<library>
<book id="b1"><title>XML</title><price>499</price></book>
<book id="b2"><title>XSLT</title><price>599</price></book>
</library>
XPath queries
- Titles:
/library/book/title - Expensive:
/library/book[price > 500] - Price of b2:
//book[@id='b2']/price/text()
Real-World Use
Practical
- Write 10 XPath expressions for your dataset.
- Verify results using an XPath tester.
đ For exams
Exam
- Define XPath and predicate.
- Explain axes with examples.
⨠Key points
Takeaways
- Predicates are the main filtering tool.
- Axes provide powerful relative navigation.