Using Abbreviations in XPath Expressions

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Developer's Guide

Using Abbreviations in XPath Expressions

Several shortcuts are available to be used in place of certain axes:

  • The default axis if none is provided in a given location step is child::. Therefore, the following two location steps are equivalent, both locate a <name> child of the context node:
    child::name
    name
  • The "at" sign, @, is a shortcut for the 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
  • Two succeeding slashes, //, is a shortcut for /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
  • A single period, ., is a shortcut for 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

  • Two succeeding periods, , are a shortcut for 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"]

  • The asterisk (*) is a wild-card character, used in a name node test to indicate that selected nodes may have any names at all. Therefore, given that various elements in orgchart.xml have the same two attributes, 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:

    @*

  • While not really a shortcut, a single leading slash, representing the document root in an XPath expression always makes it an absolute path, relative to the document root. Therefore, the following location steps are not equivalent:
    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.

Example

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>

See Also

Sample XML File for Navigating XPath Axes