Single-Node XPaths
A
single-node XPath is an XPath expression using a subset of XPath constructs and having a normalised form. A single-node XPath will always resolve to zero or one node when evaluated against any document.
A single-node XPath is composed of a sequence of
single-node XPath parts. A single-node XPath part is made up of the following sequence of tokens:
- "/"
- The node name: a qualified element name of the form "prefix:localName", or "text()"
- "["
- The index: a non-negative integer
- "]"
Expanded Form
A single-node XPath can be converted into an
expanded form by replacing all the prefixes of all the node names in the original path with the following sequence:
- " "
- The namespace URI corresponding which the prefix of the node name maps to
- " "
- The local element name of the node name
An expanded form of a single-node XPath is not a valid XPath expression. Two single-node XPaths evaluated on the same document will return the same result if and only if they convert to the same expanded form. Therefore, two single-node XPaths can be compared for equivalence without evaluating against any document.
Generating Single-Node XPaths
Given a node in a document, we can determine a single-node XPath expression for that node. We first define parent (node) and indexOfChild (parentNode, childNode) as follows:
- parent (node) is the parent node of node in the same document
- indexOfChild (parentNode, childNode) is the index of childNode in parentNode's list of child nodes
We define singleNodeXPath (node), which gives a single-node XPath for node, as follows.
- If node is the root element of the document, then singleNodeXPath (node) is: "/", the node's name, "[", 0, "]".
- Otherwise, singleNodeXPath (node) is: singleNodeXPath (parent (node)), "/", the node's name, "[", indexOfChild (parent(node), node), "]".
--
SimonMiles - 13 Mar 2006
to top