xPath
Definition
XPath
W3C standard: query language for XML
Documents as tree with 7 kinds of nodes:
- Document node - Element node - Attribute node - Text node - Namespace node - Processing-instruction node - Comment node
Location Paths
Location Path
location steps
to a node, each seperated by
/
:
axis :: node_test [predicate-1] [predicate-2] ...
axis
relationship
node_test
type of node
predicate
filter
(can be 0 or more)
Axis
Unless specified, we always select nodes and not attributes, namespaces.
self
selects origin
attribute
selects attributes for that element (but not the defined namespace)
namespace
selects namespace defined for that element
child
selects all children
descendant
selects all decendants
descendant-or-self
selects all decendants and self
parent
selects parent (can only be a single node in tree)
ancestor
selects all ancestors
ancestor-or-self
selects all ancestors and self
following
selects all following (= below the element in the XML document)
following-sibling
selects all following but without their children and grandchildren ...
preceding
reverse of “following” (= above the element in the XML document)
preceding-sibling
reverse of “following-sibling” (no grandchildren ...)
Node-Test
::node()
selects all nodes independent of axis
::
name
selects nodes with the “
name
”, based on axis
::text()
selects text
::*
selects all nodes based on axis
Predicate
Predicates are XPath expressions and they filter initial list of nodes (applied from left to right).
General XPath expressions
Location paths XPath expressions
Location paths return: node sets
XPath expressions return: number, string, boolean, node-sets
XPath operators
Syntactic Sugar
Syntactic sugar (Abbreviated Syntax) for shorter expressions.
/descendant-or-self::node()/
//
self::node()
.
parent::node()
..
child::
nothing
attribute::
@
position() = n
n
Examples of usage
/child::courses/child::course[attribute::semester] /courses/child::course[attribute::semester] /courses/course[attribute::semester] /courses/course[@semester]
/child::courses/child::course[position() = 1][attribute::semester = "Summer"] /courses/child::course[position() = 1][attribute::semester = "Summer"] /courses/course[position() = 1][attribute::semester = "Summer"] /courses/course[1][attribute::semester = "Summer"] /courses/course[1][@semester = "Summer"]
/descendant-or-self::node()/child::course[position() = 1][attribute::semester = "Summer"] //child::course[position() = 1][attribute::semester = "Summer"] //course[position() = 1][attribute::semester = "Summer"] //course[1][attribute::semester = "Summer"] //course[1][@semester = "Summer"]
(all equivalent)