<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="../../../_utils/schema/yaps.rnc" type="application/relax-ng-compact-syntax"?>
<?xml-model href="../../../_utils/schema/yaps.isosch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<?xml-stylesheet type="text/xsl" href="yaps2slidy1.xslt"?>
<!-- $Id: xpath_intro.xml 28651 2016-05-13 09:27:29Z syd $ -->
<TEI xmlns="http://www.wwp.northeastern.edu/ns/yaps" version="5.0">
  <teiHeader>
    <fileDesc>
      <titleStmt>
        <title>Navigating the Tree</title>
        <author>Julia Flanders and Syd Bauman</author>
      </titleStmt>
      <editionStmt>
        <edition>Brown University XSLT</edition>
      </editionStmt>
      <publicationStmt>
        <distributor>Women Writers Project (via website)</distributor>
        <address>
          <addrLine>url:mailto:wwp@neu.edu</addrLine>
        </address>
        <date when="2013-11-21"/>
        <availability status="restricted">
          <p>Copyright 2012 Syd Bauman, Julia Flanders, and the Women Writers Project</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>
        <pubPlace>Providence, RI USA</pubPlace>
      </publicationStmt>
      <sourceDesc>
        <p>A gentle introduction to XPath patterns</p>
      </sourceDesc>
    </fileDesc>
    <revisionDesc>
      <change who="#jflanders.lfw" when="2012-11-29">Created file from scratch</change>
    </revisionDesc>
  </teiHeader>
  <text>
    <presentation>
      <abstract>
        <p>This slide set builds on the previous XSLT tutorials by discussing XPath, which is a way
          of navigating an XML tree. It is particularly important for XSLT and publication because
          it allows the selection of specific elements depending on their context. So, for example,
          perhaps you want to render “quote” differently when it comes up in “epigraph” than when it
          occurs in “p.” XPath allows you to specify context for a given element, which makes
          transformations more nuanced.</p>
      </abstract>
      <section>
        <head>Identifying the element to match: simple identification</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic2.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>We've already seen examples of the simple case where we name an element in the value of
              <att>match</att>, and thereby we identify an element to be processed; here we say
            "please match the TEI element"</p>
          <p>We're not talking here about the flow of the stylesheet: we're just saying "when my
            turn comes, if there's a XX, I match it..."</p>
        </lectureNote>
        <tutorial>
          <p>In the previous tutorials, we have seen examples of XSL stylesheets where we name an
            element in the value of <att>match</att>, and thereby we identify an element to be
            processed; in these cases we say "please match the TEI element."</p>
          <p>In those cases we've seen, we were not talking about the flow of the stylesheet, or the
            context of the element we are trying to match. We were simply saying "when my turn
            comes, if there's an element XX, please match it..."</p>
          <p>The example here in blue says, <quote>match the TEI element.</quote></p>
        </tutorial>
      </section>

      <section>
        <head>Identifying the element to match: multiples</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic3.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>Here's an example where naming an element actually identifies multiple instances of
            that element: in this case, all the <gi>div</gi> elements in the document</p>
          <p>We can also specify multiple different elements that we want to target; the or-bar
            means "this or that", "head or opener"</p>
        </lectureNote>
        <tutorial>
          <p>There are also instances in which the <att>match</att> attribute will identify multiple
            elements in the document. For instance, in the first scenario, the <att>match</att>
            attribute matches all of the <gi>div</gi>s within the document. </p>
          <p>The second example actually shows how to select several different elements, not just
            multiple instances of the same element. The or-bar specifies "this or that." In this
            case the head or the opener should use this template. As in the first example, this will
            match head or closer elements anywhere in the document</p>
          <p>In either of these cases we might want to be more specific and say, for example, that I
            only want the <gi>div</gi> elements that appear within the <gi>front</gi> element, or I
            only want <gi>head</gi> or <gi>closer</gi> elements if they appear in the
              <gi>teiHeader</gi>; XPath provides a syntax for doing this.</p>
        </tutorial>
      </section>

      <section>
        <head>Identifying the element to match: context</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic4.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>And we can also specify particular contexts we're interested in: for instance, only the
              <gi>div</gi> elements that appear within <gi>front</gi></p>
        </lectureNote>
        <tutorial>
          <p>We can specify the the elements by the context in which they appear. In this example,
            we are only selecting <gi>div</gi> elements that are within <gi>front</gi>, and only the
              <gi>head</gi> elements that are within <gi>div</gi>s within front <gi>front</gi>.</p>
          <p>Using this simple XPath method, we can navigate the entire input document tree.</p>
        </tutorial>
      </section>

      <section>
        <head>Some other useful patterns: attribute values, order of siblings</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic5.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>We can also select contexts based on particular attribute values (similar to CSS
            selectors): basically saying "I'm looking at all the divs, and of those I only want the
            ones that have this attribute, and this value"</p>
          <p>Also based on which sibling we are talking about: e.g. we only want the first
              <gi>p</gi> child in a given context.</p>
        </lectureNote>
        <tutorial>
          <p>We can also select elements based off of their attribute values. In the first example,
            what we're saying is "We're looking at all the <gi>div</gi> elements, but we only want
            the ones that have this particular attribute and this particular value." The syntax for
            specifying this is shown in blue, and you can replace it with any element, attribute or
            value: <code>match="element[@att='value']"</code>. Note that you need to use single
            quotes for the <att>match</att> value, since using double quotes would create an
            ill-formed document.</p>
          <p>In the second example, we are specifying the matched element based on its position
            relative to its sibling elements. Adding a number in square brackets after the element
            name in <att>match</att> allows you to specify which number element you want to select.
            So, for example p[1] would only select and match the first paragraph within a given
            element.</p>
        </tutorial>
      </section>

      <section>
        <head>How to suppress parts of the input tree</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic6.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>We can also suppress whole branches of the input tree, basically telling the XSLT
            processor to ignore them</p>
          <p>We do this by providing a template that matches the parent element for that branch, but
            with no "apply templates" instruction</p>
        </lectureNote>
        <tutorial>
          <p>You can suppress parts of the input tree by giving the <att>match</att> attribute of
              <gi>xsl:template</gi> the value of the element you are trying to suppress. Then leave
            the content of this <gi>xsl:template</gi> empty. By leaving out the
              <gi>apply-templates</gi> instruction, the processor is instructed to ignore this part
            of the input tree (the matched element and all of its descendants). You can either use
            the syntax given, or <tag>xsl:templates match="something"/</tag> (the alternate way of
            writing empty elements).</p>
          <p>In the example, the template supresses the <gi>teiHeader</gi> and all of its
            descendents. This is something you might do if the metadata included in the
              <gi>teiHeader</gi> is not relevant in the ultimate output document, like HTML.</p>
        </tutorial>
      </section>

      <section>
        <head>More complex navigation: the context node</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic7.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>We've been doing some simple navigation so far, basically ignoring the question of
            where exactly we are in the tree</p>
          <p>However, to do more complex navigation and selection of nodes for processing, we have
            to have a more complex understanding of how we navigate, which involves knowing where
            the XSLT processor thinks we are (and hence, how to get to where we want to go
              <emph>from there</emph>)</p>
          <p>In an XSLT template, when we match an element, whatever else happens inside that
            template happens relative to the location of that element (i.e. the matched element in
            the input tree); that element is "the context node".</p>
          <p>So for instance in this example, our template is selecting <gi>div</gi> as the context
            node, and as a result the <gi>p</gi> elements identified by the <att>select</att>
            attribute are limited to those within the context node: the <gi>p</gi> children of the
            context node</p>
        </lectureNote>
        <tutorial>
          <p>So far we’ve been doing some simple navigation and we've basically ignored the question
            of where exactly we are in the tree. However, to select nodes for more complex
            processing, we have to have a more complex understanding of how we navigate, which
            involves knowing where the XSLT processor thinks we are. This will help us know how to
            get to where we want to go from there. </p>
          <p>In an XSLT template, when we match an element, whatever else happens inside that
            template happens relative to the location of that element (i.e. the matched element in
            the input tree); that element is <quote>the context node</quote>. </p>
          <p>In this example, our template is using the <att>match</att> attribute to select
              <gi>div</gi> as the context node, and as a result the <gi>p</gi> elements identified
            by the select attribute are limited to those within the context node: the <gi>p</gi>
            children of the context node.</p>
        </tutorial>
      </section>

      <section>
        <head>Navigating from the root of the tree</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic7a.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>If we want to navigate from the root, rather than relative to our current position,
            start the expression with a slash</p>
        </lectureNote>
        <tutorial>
          <p>If we want to navigate from the root, rather than the current node, we start our
            expression with a slash. Starting from the root node is useful if you want to apply
            templates to elements from a different part of the input document tree, as in the
            example here.</p>
        </tutorial>
      </section>

      <section>
        <head>Parents, ancestors, descendants, and children</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic8.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>So far we've just been navigating down the tree in a very simple way: all of our
            matches and selections thus far have been done using just the element name. It's time to
            gloss this notation in a bit more detail: an element name X (used on the
              <att>select</att> attribute, all by itself, means "the X children of the context
            node", "my X children"</p>
          <p>If we want the descendants, not just the children, there's another notation: //</p>
          <p>And if we want parents, there's another notation ../</p>
          <p>And if we want ancestors, there's another (more verbose) notation</p>
        </lectureNote>
        <tutorial>
          <p>So far we've just been navigating down the tree in a very simple way: all of our
            matches and selections thus far have been done using just the element name. It's time to
            gloss this notation in a bit more detail: an element name X (used on the select
            attribute, all by itself, means "the X children of the context node," "my X
            children."</p>
          <p>If we want the descendants, not just the children, there's another notation:
              <code>//</code>. So in the first example we get all the <gi>p</gi> elements that are
            descendants of <gi>text</gi> (highlighted in purple).</p>
          <p>And if we want parents, there's another notation <code>..</code> In the second example
            we get the parent element of <gi>text</gi>, which is <gi>TEI</gi> (highlighted in
            orange).</p>
          <p> And if we want some child of a parent we can combine notation as seen in
              <code>../teiHeader</code>. In the third example, we go up to the parent element of
              <gi>text</gi>, which is <gi>TEI</gi>, then select the element <gi>teiHeader</gi> that
            is a child of <gi>TEI</gi> (highlighted in green).</p>
          <p>Finally, if we want ancestors, there's another (more verbose) notation
              <code>ancestor::TEI</code>. In the final example, we select <gi>TEI</gi>, an ancestor
            of <gi>p</gi> (highlighted in orange). </p>
        </tutorial>
      </section>

      <section>
        <head>Following and preceding siblings</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic9.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>The idea of parents and ancestors, children and descendants, takes us up and down the
            tree; we can think of these relationships as traversing <term>axes</term> that radiate
            out from the context node</p>
          <p>We can also go across the tree along horizontal axes: to identify
              <emph>preceding</emph> and <emph>following</emph> nodes</p>
          <p>A simple case: the following sibling...</p>
        </lectureNote>
        <tutorial>
          <p>Parents and ancestors take us up and down the tree. The relationships are like axes
            that radiate out from the context node. However, we can also go along the tree using the
            horizontal axes, identifying following and preceding nodes. The case in the example here
            is the following sibling, meaning any sibling, <gi>p</gi>, that occurs after the context
            node (<gi>head</gi>).</p>
        </tutorial>
      </section>

      <section>
        <head>Following and preceding nodes</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic10.png"/>
          </figure>
        </slide>
        <lectureNote>
          <p>The more general case of "following" nodes are a little trickier because you have to
            remember that an element's child doesn't "follow" it. To find "following" elements you
            have to go up one level, to the parent, as in this example: the "following" <gi>div</gi>
            elements are those that are children of any following siblings of the context node.</p>
        </lectureNote>
        <tutorial>
          <p>The more general case of "following" nodes is a bit trickier because you have to
            remember that an element's child doesn't "follow" it. It may appear to in terms of the
            layout of your XML file, but that's not how the tree structure is understood to
            work.</p>
          <p>In the cases of following and preceding nodes, we need to look at all of the elements
            that occur outside (either before or after) the context node's boundaries. So the first
            example, all of the <gi>div</gi> elements that come after <gi>front</gi> are processed
            with the first template (in blue). In the second example, you need to look at all of the
              <gi>div</gi> elements that come before the <gi>body</gi> element. All of these
              <gi>div</gi> elements are processed with the instructions in pink. Remember that
            following and preceding elements are never children or descendants of the element
            specified as context.</p>
        </tutorial>
      </section>

      <section>
        <head>Axes</head>
        <slide>
          <figure>
            <graphic height="600px" url="../../../_utils/gfx/xpath_basic11.png"/>
          </figure>
          <p>For reference, <ref target="../../handouts/xpath_cribsheet.xhtml">a cribsheet for
              XPath</ref></p>
        </slide>
        <lectureNote>
          <p>Let's take a step back and look at the axes themselves</p>
          <p>For reference, <ref target="../../handouts/xpath_cribsheet.xhtml">a cribsheet for
              XPath</ref></p>
        </lectureNote>
        <tutorial>
          <p>The different axes that XPath employs are shown on this slide. In this tutorial we have
            covered ways to navigate these axes using XPath. Take a minute to look through this XML
            tree diagram, to acquaint yourself with the ways in which different XML elements relate
            to each other.</p>
          <!-- ancestor is spelled incorrectly in the graphic? -->
          <list>
            <head>This tutorial is complete, please see links below to continue:</head>
            <item><ref target="./conditionals_tutorial_00.xhtml">Proceed to next tutorial in Transformation
                and Publication Primer</ref></item>
            <item><ref target="../../../../resources/transformation.html">Return to Transformation
                and Publication Primer</ref></item>
            <item><ref target="../../../../resources/tutorial_main.html">Return to main tutorial
                page</ref></item>
          </list>
        </tutorial>
      </section>

    </presentation>
  </text>
</TEI>
