<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="../../_utils/stylesheets/admin-tei.css"?>
<?oxygen RNGSchema="../../_utils/schema/handout.rnc" type="compact"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0" version="5.0">
  <teiHeader>
    <fileDesc>
      <titleStmt>
        <title>XPath Crib Sheet</title>
        <author xml:id="sdb">Syd Bauman</author>
        <author xml:id="djb">David Birnbaum</author>
        <author xml:id="jhf">Julia Flanders</author>
      </titleStmt>
      <publicationStmt>
        <publisher>Northeastern University Women Writers Project</publisher>
        <availability status="restricted">
          <p>Copyright 2011 Syd Bauman, David Birnbaum, Julia
	  Flanders, and WWP</p>
          <p>This TEI-encoded XML file is available under the terms of
	  the <ref target="http://creativecommons.org/licenses/by-sa/3.0/">Creative
	  Commons Attribution-ShareAlike 3.0 (Unported)</ref>
	  license.</p>
        </availability>
      </publicationStmt>
      <sourceDesc>
        <p>Quick guide to XPath path expressions.</p>
      </sourceDesc>
    </fileDesc>
    <revisionDesc>
      <change when="2012-12-05" who="#sdb">
        <list>
          <item>fix author metadata</item>
          <item>change <gi>eg</gi> to <gi>code</gi> passim</item>
        </list>
      </change>
      <change when="2012-12-04" who="#sdb">
	Swapped columns 3 &amp; 4, dropped most (now useless) comments
      </change>
      <change when="2012-12-04" who="#jhf"/>
    </revisionDesc>
  </teiHeader>
  <text>
    <body>
      <head>Crib Sheet for XPath Expressions</head>
      <table rend="class( bordered )">
        <row>
          <cell>short</cell>
          <cell>long</cell>
          <cell>example &#x2026;</cell>
          <cell>&#x2026; means</cell>
        </row>
        <row>
          <cell>
            <code rend="css(font-weight:bold;)">.</code>
          </cell>
          <cell>
            <code>self::node()</code>
            <lb/>
            <note rend="class(dull)">(note: long and short are not exactly equivalent)</note>
          </cell>
          <cell/>
          <cell>me, whatever I happen to be (element, attribute,
	  comment, processing instruction, document root, or even
	  text)</cell>
          <!-- XPath 2.0 says that "." is the ContextItemExpr (production 47) -->
          <!-- It may be a node or an atomic value; thus self::node() is the -->
          <!-- same iff the context item is a node. -->
        </row>
        <row>
          <cell>
            <rs type="generic">element-name</rs>
          </cell>
          <cell>
            <code>child::</code>
            <rs type="generic">element-name</rs>
          </cell>
          <cell>
            <code>&lt;xsl:apply-templates select="said"&gt;</code>
          </cell>
          <cell>my <gi>said</gi> children</cell>
        </row>
        <row>
          <cell>
            <code>*</code>
          </cell>
          <cell>
            <code>child::*</code>
          </cell>
          <cell>
            <code>&lt;xsl:template match="*"&gt;</code>
          </cell>
          <cell>match <emph>each</emph> child element</cell>
        </row>
        <row>
          <cell>
            <code>//</code>
            <rs type="generic">element-name</rs>
          </cell>
          <cell>
            <code>descendant::</code>
            <rs type="generic">element-name</rs>
            <lb/>
            <note rend="class(dull)">(note: long and short are not exactly equivalent)</note>
          </cell>
          <cell>
            <code>&lt;xsl:template match="body//div"&gt;</code>
          </cell>
          <cell>the <gi>div</gi> descendants of <gi>body</gi> (i.e.,
	  all <gi>div</gi> elements, except those that are somewhere
	  inside a <gi>front</gi> or <gi>back</gi>)</cell>
          <!-- //div == /descendant-or-self::node()/child::div -->
          <!-- Thus //div[1] != /descendant::div[1] -->
          <!-- //div[1] means "get me every <div> that is a first child" -->
          <!-- /descendant::div[1] means "get me first <div> in doc" -->
        </row>
        <row>
          <cell>
            <code rend="css(font-weight:bold;)">..</code>
          </cell>
          <cell>
            <code>parent::node()</code>
          </cell>
          <cell/>
          <cell>my parent, whatever it happens to be (element or root)</cell>
        </row>
        <row>
          <cell/>
          <cell>
            <code>parent::</code>
            <rs type="generic">element-name</rs>
          </cell>
          <cell>
            <eg rend="css(background-color:white;)"><![CDATA[<xsl:if test="parent::author">
  <!-- stuff -->
</xsl:if>]]></eg>
          </cell>
          <cell>do “stuff” iff my parent is an <gi>author</gi></cell>
        </row>
        <row>
          <cell/>
          <cell>
            <code>ancestor::</code>
            <rs type="generic">element-name</rs>
          </cell>
          <cell>
            <code>&lt;xsl:apply-templates select="ancestor::listPerson/head"&gt;</code>
          </cell>
          <cell>process my ancestor <gi>listPerson</gi>s’ headings</cell>
        </row>
        <row>
          <cell/>
          <cell>
            <code>ancestor::*</code>
          </cell>
          <cell>
            <code>&lt;xsl:apply-templates select="ancestor::*"&gt;</code>
          </cell>
          <cell>all my ancestors</cell>
        </row>
        <row>
          <cell/>
          <cell>
            <code>following::</code>
            <rs type="generic">element-name</rs>
          </cell>
          <cell>
            <code>&lt;xsl:apply-templates select="following::sic"&gt;</code>
          </cell>
          <cell>Process <gi>sic</gi>s after me (e.g., to make a list of errors left to go)</cell>
        </row>
        <row>
          <cell/>
          <cell>
            <code>following-sibling::</code>
            <rs type="generic">element-name</rs>
          </cell>
          <cell>
            <code>&lt;xsl:apply-templates select="following-sibling::l"&gt;</code>
          </cell>
          <cell>the <gi>l</gi> children of my parent that come after
	  me (i.e., the rest of the stanza)</cell>
        </row>
        <row>
          <cell/>
          <cell>
            <code>preceding::</code>
            <rs type="generic">element-name</rs>
          </cell>
          <cell>
            <code>&lt;xsl:value-of select="count( preceding::pb )"&gt;</code>
          </cell>
          <cell>what page # I’m on</cell>
        </row>
        <row>
          <cell/>
          <cell>
            <code>preceding-sibling::</code>
            <rs type="generic">element-name</rs>
          </cell>
          <cell>
            <code>&lt;xsl:apply-templates select="preceding-sibling::head"&gt;</code>
          </cell>
          <cell>the <gi>head</gi> children of my parent that come before me</cell>
        </row>
        <row>
          <cell>
            <code>@</code>
            <rs type="generic">attribute-name</rs>
          </cell>
          <cell>
            <code>attribute::</code>
            <rs type="generic">attribute-name</rs>
          </cell>
          <cell>
            <code>&lt;xsl:template match="@rend"&gt;</code>
          </cell>
          <cell>handle <att>rend</att> attributes</cell>
        </row>
        <row>
          <cell>
            <code>@*</code>
          </cell>
          <cell>
            <code>attribute::*</code>
          </cell>
          <cell>
            <code>&lt;xsl:apply-templates select="@*"&gt;</code>
          </cell>
          <cell>process all of my attributes</cell>
        </row>
      </table>
    </body>
  </text>
</TEI>
