XPath Crib Sheet

Crib Sheet for XPath Expressions

short long example … … means
. self::node()
(note: long and short are not exactly equivalent)
me, whatever I happen to be (element, attribute, comment, processing instruction, document root, or even text)
element-name child::element-name <xsl:apply-templates select="said"> my said children
* child::* <xsl:template match="*"> match each child element
//element-name descendant::element-name
(note: long and short are not exactly equivalent)
<xsl:template match="body//div"> the div descendants of body (i.e., all div elements, except those that are somewhere inside a front or back)
.. parent::node() my parent, whatever it happens to be (element or root)
parent::element-name
<xsl:if test="parent::author">
  <!-- stuff -->
</xsl:if>
do “stuff” iff my parent is an author
ancestor::element-name <xsl:apply-templates select="ancestor::listPerson/head"> process my ancestor listPersons’ headings
ancestor::* <xsl:apply-templates select="ancestor::*"> all my ancestors
following::element-name <xsl:apply-templates select="following::sic"> Process sics after me (e.g., to make a list of errors left to go)
following-sibling::element-name <xsl:apply-templates select="following-sibling::l"> the l children of my parent that come after me (i.e., the rest of the stanza)
preceding::element-name <xsl:value-of select="count( preceding::pb )"> what page # I’m on
preceding-sibling::element-name <xsl:apply-templates select="preceding-sibling::head"> the head children of my parent that come before me
@attribute-name attribute::attribute-name <xsl:template match="@rend"> handle rend attributes
@* attribute::* <xsl:apply-templates select="@*"> process all of my attributes