<?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: djb_grouping.xml 51709 2026-05-28 23:29:13Z syd $ -->
<TEI xmlns="http://www.wwp.northeastern.edu/ns/yaps"
     xmlns:y="http://www.wwp.northeastern.edu/ns/yaps"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <teiHeader>
    <fileDesc>
      <titleStmt>
        <title>Grouping in XSLT</title>
        <author>David J. Birnbaum</author>
      </titleStmt>
      <editionStmt>
        <edition>Introduction to XSLT for Digital Humanities, Brown University, July 20-22, 2011</edition>
      </editionStmt>
      <publicationStmt>
        <distributor>Women Writers Project (via website)</distributor>
        <address>
          <addrLine>mailto:wwp@northeastern.edu</addrLine>
        </address>
        <date when="2011-07-22"/>
        <availability status="restricted">
          <p>Copyright 2011 David J. Birnbaum 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>Demonstration of grouping in XSLT 2.0</p>
      </sourceDesc>
    </fileDesc>
  </teiHeader>
  <text>
    <presentation>
      <section>
        <head>Why use grouping?</head>
        <slide>
          <p>Four subtypes: <code>group-starting-with</code>,
          <code>group-ending-with</code>, <code>group-by</code>,
          <code>group-adjacent</code></p>
          <list>
            <item><code>group-starting-with</code>: unflatten (X)HTML (example to
            follow)</item>
            <item><code>group-by</code>: arrange a bibliography into a hierarchy by
            author or year</item>
            <item><code>group-ending-with</code> and <code>group-adjacent</code> are
            less common</item>
          </list>
        </slide>
      </section>
      <section>
        <head>Marsupials</head>
        <slide>
          <table>
            <row>
              <cell>Bilby</cell>
              <cell>Koala</cell>
              <cell>Wombat</cell>
            </row>
            <row>
              <cell><figure>
                <figDesc>Bilby goes here</figDesc>
                <graphic url="../../../_utils/gfx/bilby.jpg" width="100%"/>
              </figure></cell>
              <cell><figure>
                <figDesc>Koala goes here</figDesc>
                <graphic url="../../../_utils/gfx/koala.jpg" width="100%"/>
              </figure></cell>
              <cell><figure>
                <figDesc>Wombat goes here</figDesc>
                <graphic url="../../../_utils/gfx/wombat.jpg" width="100%"/>
              </figure></cell>
            </row>
          </table>
        </slide>
      </section>
      <section>
        <head>Monotremes</head>
        <slide>
          <table>
            <row>
              <cell>Echidna</cell>
              <cell>Platypus</cell>
            </row>
            <row>
              <cell><figure>
                <figDesc>Echidna goes here</figDesc>
                <graphic url="../../../_utils/gfx/echidna.jpg" width="100%"/>
              </figure></cell>
              <cell><figure>
                <figDesc>Platypus goes here</figDesc>
                <graphic url="../../../_utils/gfx/platypus.jpg" width="100%"/>
              </figure></cell>
            </row>
          </table>
        </slide>
      </section>

      <section>
        <head>An XHTML view of some Australian animals</head>
        <slide>
          <egMarkup scheme="XHTML" type="valid">
            <html y:xmlns="http://www.w3.org/1999/xhtml">
              <head>
                <title>Australian animals</title>
              </head>
              <body>
                <h1>Australian animals</h1>
                <h2>Marsupials</h2>
                <ul>
                  <li>Bilby</li>
                  <li>Koala</li>
                  <li>Wombat</li>
                </ul>
                <h2>Monotremes</h2>
                <ul>
                  <li>Echidna</li>
                  <li>Platypus</li>
                </ul>
              </body>
            </html>
          </egMarkup>
          <p>Task: Find all the marsupials</p>
        </slide>
      </section>

      <section>
        <head>An XHTML view of some Australian animals</head>
        <slide>
          <egMarkup scheme="XHTML" type="valid">
            <html y:xmlns="http://www.w3.org/1999/xhtml">
              <head>
                <title>Australian animals</title>
              </head>
              <body>
                <h1>Australian animals</h1>
                <h2>Marsupials</h2>
                <ul>
                  <li>Bilby</li>
                  <li>Koala</li>
                  <li>Wombat</li>
                </ul>
                <h2>Monotremes</h2>
                <ul>
                  <li>Echidna</li>
                  <li>Platypus</li>
                </ul>
              </body>
            </html>
          </egMarkup>
          <p>Solution: <code>//li[preceding::h2[1] eq 'Marsupials']</code></p>
          <p>Problem: depends on order and user knowledge of order,
          which is fragile and non-informational</p>
        </slide>
      </section>
      <section>
        <head>An XML view of some Australian animals</head>
        <slide>
          <egMarkup scheme="ad-hoc" type="valid">
            <animals>
              <order label="Marsupials">
                <animal>Bilby</animal>
                <animal>Koala</animal>
                <animal>Wombat</animal>
              </order>
              <order label="Monotremes">
                <animal>Echidna</animal>
                <animal>Platypus</animal>
              </order>
            </animals>
          </egMarkup>
          <p>Task: Find all the marsupials</p>
        </slide>
      </section>
      <section>
        <head>An XML view of some Australian animals</head>
        <slide>
          <egMarkup scheme="ad-hoc" type="valid">
            <animals>
              <order label="Marsupials">
                <animal>Bilby</animal>
                <animal>Koala</animal>
                <animal>Wombat</animal>
              </order>
              <order label="Monotremes">
                <animal>Echidna</animal>
                <animal>Platypus</animal>
              </order>
            </animals>
          </egMarkup>
          <p>Solution: <code>//order[@label eq 'Marsupials']/animal</code></p>
        </slide>
      </section>
      <section>
        <head>Task: Convert XHTML to XML</head>
        <slide>
          <p><emph>When and why?</emph> You inherit XHTML and want to convert it to
          something easier to work with and easier to maintain</p>
          <p><emph>What’s the problem?</emph> Difficult because the input is flat and the
          output hierarchical</p>
          <egMarkup scheme="XSLT" type="valid">
            <xsl:for-each-group select="//h2 | //li"
                                group-starting-with="h2">
              <!-- ... -->
            </xsl:for-each-group>
          </egMarkup>
          <p>Selects all <gi>h2</gi> and <gi>li</gi> elements</p>
          <p>Each <gi>h2</gi> begins a new group, which continues until the next
          <gi>h2</gi> or the end of the <gi>li</gi>s</p>
          <p>Refer to the current group as <code>current-group()</code></p>
        </slide>
      </section>
      <section>
        <head>Solution</head>
        <slide>
          <egMarkup scheme="XSLT" type="valid">
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
                            xpath-default-namespace="http://www.w3.org/1999/xhtml">
              <xsl:output method="xml" indent="yes"/>
              <xsl:template match="/">
                <animals>
                  <!-- we want only h2 (orders) and li (animals)
                       the animals in each order (li) follow the order (h2)-->
                  <xsl:for-each-group select="//h2 | //li" group-starting-with="h2">
                    <!-- process first item in group (h2) -->
                    <xsl:apply-templates select="."/>
                  </xsl:for-each-group>
                </animals>
              </xsl:template>
              <xsl:template match="h2">
                <!-- it's an order, so create an <order> element
                     with an attribute bearing the name of the order-->
                <order label="{.}">
                  <!-- process everything in the group except the h2 -->
                  <xsl:apply-templates select="current-group() except ."/>
                </order>
              </xsl:template>
              <xsl:template match="li">
                <!-- create an <animal> for each animal (li) in the input -->
                <animal>
                  <xsl:apply-templates/>
                </animal>
              </xsl:template>
            </xsl:stylesheet>
          </egMarkup>
        </slide>
      </section>

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