XML
Beginner
1 min read
XPath Axes and Node Tests
Example
<!--
XPath Axis Examples
(using the library document from the previous lesson)
-->
<!--
CHILD AXIS (default):
child::book = book
child::* = * (all element children)
child::text() = text node children
DESCENDANT AXIS:
descendant::title = all <title> at any depth
//title = abbreviated form
PARENT AXIS:
.. = abbreviated parent::node()
parent::library = parent if it is <library>
ANCESTOR AXIS:
ancestor::* = all ancestor elements
SIBLING AXES:
following-sibling::book = next <book> siblings
preceding-sibling::book[1] = the nearest preceding <book>
ATTRIBUTE AXIS:
@id = attribute::id
@* = all attributes
SELF AXIS:
. = self::node()
self::book = true only if context node is <book>
Combined step examples:
/library/book/following-sibling::magazine → m001
//book[@id='b001']/following-sibling::* → b002, m001
//title/parent::book/@id → b001, b002
//book[author='Robert C. Martin']/@category → non-fiction
Namespace-aware:
//*[local-name()='subject'] → dc:subject element
//*[namespace-uri()='http://purl.org/dc/elements/1.1/']
→ dc:subject
-->