Several shortcuts are available to be used in place of certain axes:
child::
. Therefore, the following two location steps are equivalent, both locate a <name>
child of the context node:
child::name name
attribute::
axis. Therefore, the following two location steps are equivalent, both locate the empdate
attribute of a <director>
child of the context node:
director/attribute::empdate ddirector/@empdate
/descendant-or-self::node()/
.Therefore, the following two location steps are equivalent, locating both the node whose name is "name" and whose string-value is "Kim Akers," and all its descendants, except attribute and namespace nodes:
name[.="Kim Akers"]/descendant-or-self::node()/name name[.="Kim Akers"]//name
self::node()
. Therefore, the following two location steps, which locate an attribute of the context node, are equivalent:
self::node()/attribute::empdate ./attribute::empdate
This XPath location step can be further abbreviated using the shortcut for the attribute::
axis, for example:
./@empdate
parent::node()
. Therefore, the following two location steps are equivalent, both select the parent of the context node, when the parent's value is "Josh Barnett":
parent::node()[self::node()="Josh Barnett"] ..[self::node()="Josh Barnett"]
This location step could be abbreviated even further, using the shortcut for self::node()
, as follows:
..[.="Josh Barnett"]
empID
and empdate
, the following two location steps are equivalent, assuming the context node to be one of these elements:
attribute::empID | attribute::empdate attribute::*
This location step could be abbreviated even further, using the shortcut for the attribute::
axis, as follows:
@*
name /name
The first selects all <name>
elements which are children of the context node; the second, all <name>
elements which are children of the root node, that is, it selects an empty node-set, since the root node's only child is the <chairman>
element.
This example doesn't generate significant output. It demonstrates that no errors are generated by the above XPath expressions.
XML File (booksxpath.xml)
Use the orgchart.xml (in Sample XML File for Navigating XPath Axes) and change its href
attribute to point to xpabbr.xsl.
XSLT File (xpabbr.xsl)
<?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- suppress text nodes not covered in subsequent template rule --> <xsl:template match="text()"/> <xsl:template match="president"> <p/>child::name -- <xsl:value-of select='child::name'/> <p/>name -- <xsl:value-of select='name'/> <p/>director/attribute::empdate -- <xsl:value-of select='director/attribute::empdate'/> <p/>ddirector/@empdate -- <xsl:value-of select='ddirector/@empdate'/> <p/>name[.="Kim Akers"]/descendant-or-self::node()/name -- <xsl:value-of select='name[.="Kim Akers"]/descendant-or-self::node()/name'/> <p/>name[.="Kim Akers"]//name -- <xsl:value-of select='name[.="Kim Akers"]//name'/> <p/>self::node()/attribute::empdate -- <xsl:value-of select='self::node()/attribute::empdate'/> <p/>./attribute::empdate -- <xsl:value-of select='./attribute::empdate'/> <p/>./@empdate -- <xsl:value-of select='./@empdate'/> <p/>parent::node()[self::node()="Josh Barnett"] -- <xsl:value-of select='parent::node()[self::node()="Josh Barnett"]'/> <p/>..[self::node()="Josh Barnett"] -- <xsl:value-of select='..[self::node()="Josh Barnett"]'/> <p/>..[.="Josh Barnett"] -- <xsl:value-of select='..[.="Josh Barnett"]'/> <p/>attribute::empID | attribute::empdate -- <xsl:value-of select='attribute::empID | attribute::empdate'/> <p/>attribute::* -- <xsl:value-of select='attribute::*'/> <p/>@* -- <xsl:value-of select='@*'/> <p/>name -- <xsl:value-of select='name'/> <p/>/name -- <xsl:value-of select='/name'/> </xsl:template> </xsl:stylesheet>
Sample XML File for Navigating XPath Axes