Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
An XPath expression contains one or more "location steps", separated by slashes. Each location step has
the following form:
axis-name :: node-test [predicate]*
Or in English: an axis name, then two colons, then a node-test and finally zero or more predicates in brackets.
We will show a list of valid axes and a list of valid node-sets in this appendix. A predicate is an expression. It exists of
values, operators and other XPath expressions.
The XPath axis contains a part of the document, defined from the perspective of the "context node". The node
test makes a selection from the nodes on that axis. By adding predicates, it is possible to select a subset from these nodes.
If the expression in the predicate returns true, the node remains in the selected set, otherwise it is removed.
XPath defines a set of functions for use in predicates. These are listed in the appendix as well.
Contains all direct children of the context node (that is the children, but not any of the children's children)
(default axis)
element
X
X
X
X
X
X
parent
Contains the direct parent node (and only the direct parent node) of the context node.
..
element
X
X
X
X
X
X
descendant
All children of the context node, including all children's children recursively
//
element
X
X
X
X
X
X
ancestor
Contains the context node's parent node, its parent's parent node etc., all the way up to the document root. If the context node is the root node, the ancestor node is empty.
element
X
X
X
X
X
attribute
Contains all attributes on the context node. The axis will be empty unless the context node is an element.
@
attribute
X
X
X
X
X
ancestor-or-self
Identical to the ancestor axis, but including the context node itself.
element
X
X
X
X
X
descendant-or-self
Identical to the descendant axis, but including the context node itself.
element
X
X
X
X
X
self
Contains only the context node itself.
.
element
X
X
X
X
X
X
preceding-sibling
Contains all siblings (children of the same parent node) of the context node that come before the context node in document order
element
X
X
X
X
following-sibling
Contains all siblings (children of the same parent node) of the context node that come after the context node in document order
element
X
X
X
X
namespace
Contains all namespaces available on the context node. This includes the default namespace and the xml namespace (these are automatically declared in any document). The axis will be empty unless the context node is an element.
namespace
X
X
X
X
following
Contains all nodes that come after the context node in the document order. This means that the start tag of the node must come after the closing tag of the context node and therefore excludes the descendants of the context node.
element
X
X
X
X
preceding
Contains all nodes that come before the context node in the document order. This includes all elements that are already closed (their closing tag comes before the context node in the document) and therefore excludes all ancestors of the context node.
Select ancestor elements of the context node that are named 'Chapter'
ancestor::Chapter
Select nodes that have more than two direct children of name 'Skip'
/descendant::node()[count(child::Skip) > 2]
Select all Student elements whose name attribute start with an A (using shorthand notation)
//Student[starts-with(@name, 'A')]
Note: You can now download the full content of the XPath reference, the XSLT reference and the
DOM reference in one PDF document. This document contains the implementation tables, internal
links to navigate through the references, a full linked table of contents. You can use free text
search through the whole reference or print as a nicely formatted document.
Download here...