XPath Axes (Axes)

XML-instantiedocument

We zullen in onderstaande voorbeelden dit XML-document gebruiken:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>
<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>
</bookstore>

XPath-as

De as kan een verzameling knopen definiëren ten opzichte van de huidige knoop.

Asnaam Results
ancestor Kies alle voorouders (zoals ouders, grootouders, enz.) van de huidige knoop.
ancestor-or-self Kies alle voorouders (zoals ouders, grootouders, enz.) van de huidige knoop en de huidige knoop zelf.
attribute Select all attributes of the current node.
child Select all child elements of the current node.
descendant Kies alle nakomelingen van de huidige knoop (zoals kinderen, kleinkinderen, enz.).
descendant-or-self Kies alle nakomelingen van de huidige knoop (zoals kinderen, kleinkinderen, enz.) en de huidige knoop zelf.
following Kies alle knopen na de eindtag van de huidige knoop in het document.
namespace Kies alle namespace-knopen van de huidige knoop.
parent Kies de ouderknoop van de huidige knoop.
preceding Kies alle knopen voor de starttag van de huidige knoop in het document.
preceding-sibling Kies alle同级节点 van de huidige knoop.
self Kies de huidige knoop.

Padpositie-expressie

De padpositie kan absoluut of relatief zijn.

De absolute pad start met een rechte streepje (/), terwijl de relatieve pad niet zo is. In beide gevallen omvat de padpositie een of meerdere stappen, die allemaal worden gescheiden door een rechte streepje:

Absolute Position Path:

/step/step/...

Relative Position Path:

step/step/...

Each step is calculated based on the nodes in the current node set.

Step (step) includes:

Axis (axis)
Define the tree relationship between the selected node and the current node
Node Test (node-test)
Identify nodes within a specific axis
Zero or more predicates (predicates)
Refine the selected node set more deeply

Step Syntax:

Axis Name::Node Test[Predicate]

Instance

Example Results
child::book Select all book elements that belong to the child elements of the current node.
attribute::lang Select the lang attribute of the current node.
child::* Select all child elements of the current node.
attribute::* Select all attributes of the current node.
child::text() Select all text child nodes of the current node.
child::node() Select all child nodes of the current node.
descendant::book Select all descendants of the book element
ancestor::book Select all ancestors of the book element
ancestor-or-self::book Select all ancestors of the book element and the current node (if this node is a book node)
child::*/child::price Select all price descendants of the current node.