Re: [xsl] xsl:sort and xsl:count issues (solution, long message)

Subject: Re: [xsl] xsl:sort and xsl:count issues (solution, long message)
From: "Jay Bryant" <jay@xxxxxxxxxxxx>
Date: Wed, 18 May 2005 17:54:30 -0500
Hi, Rolando,

You actually have a multi-level grouping problem. I have used a variant of
Ken Holman's multi-variable approach (mentioned a couple times in the last
few weeks on this list if you need more examples) to create a solution.

When I apply this XSL file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:template match="/">
    <html>
      <body>
        <table>
          <!-- Put all the distinct type values into a variable -->
          <xsl:variable name="types"
select="//type[not(following::type=.)]"/>
          <xsl:for-each select="$types">
            <tr>
              <td colspan="4">Type: <xsl:value-of select="."/></td>
            </tr>
            <!-- For each type, create a list of distinct creators who made
a work of that type -->
            <xsl:variable name="currentType" select="."/>
            <xsl:variable name="creators"
select="//oai_dc[type=$currentType]/creator[not(following::oai_dc[type=$curr
entType]/creator=.)]"/>
            <!-- Now that we have our trusty gang of creators, make listings
per creator -->
            <xsl:for-each select="$creators">
              <xsl:sort/>
              <xsl:variable name="currentCreator" select="."/>
              <xsl:for-each
select="//record[metadata/oai_dc/type=$currentType][metadata/oai_dc/creator=
$currentCreator]">
                <tr>
                  <td><xsl:value-of select="$currentCreator"/></td>
                  <td><xsl:value-of select="metadata/oai_dc/title"/></td>
                  <td><xsl:value-of select="metadata/oai_dc/date"/></td>
                  <td><xsl:value-of
select="metadata/oai_dc/publisher"/></td>
                </tr>
              </xsl:for-each>
            </xsl:for-each>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

To this XML file (yours extended to have more records but with some trimming
for brevity's sake):

<?xml version="1.0" encoding="UTF-8"?>
<OAI-PMH>
  <ListRecords>
    <record>
      <header>
        <identifier>oai:jce.divched.org:jcedlib-58</identifier>
        <datestamp>2003-06-03</datestamp>
      </header>
      <metadata>
        <oai_dc>
          <title>Journal of Chemical Education Digital Library</title>
          <creator>John W. Moore</creator>
          <creator>Jon L. Holmes</creator>
          <creator>Edward W. Vitz</creator>
          <creator>William F. Coleman</creator>
          <creator>Theresa J. Zielinski</creator>
          <creator>Betty Wohnt</creator>
          <description>The JCE Digital Library (JCE DLib) was established in
2003 as a result of an NSF grant. It is a part of the NSDL, and initially
includes four features: WebWare, DigiDemos, QBank, and
SymMath.</description>
          <publisher>The Division of Chemical Education, Inc., of the
American Chemical Society</publisher>
          <date>2003-01-01</date>
          <type>Collection</type>
          <format>text/html</format>
          <identifier>http://jce.divched.org/JCEDLib/</identifier>
          <language>en</language>
          <relation> isPartOf: ISSN:0021-9584</relation>
          <rights>Copyright 2003 by the Division of Chemical Education,
Inc., American Chemical Society.</rights>
          <subject>Elementary / Middle School</subject>
        </oai_dc>
      </metadata>
    </record>
    <record>
      <header>
        <identifier>oai:jce.divched.org:jcedlib-58</identifier>
        <datestamp>2003-06-03</datestamp>
      </header>
      <metadata>
        <oai_dc>
          <title>A Much More Fun Book</title>
          <creator>Walt Disney</creator>
          <creator>Dalt Wisney</creator>
          <creator>Betty Wohnt</creator>
          <description>Gotta be better than chemistry</description>
          <publisher>The Division of More Fun Than Chemistry, Inc., of the
American Anything Else Society</publisher>
          <date>2003-01-01</date>
          <type>Collection</type>
          <format>text/html</format>
          <identifier>http://jce.divched.org/JCEDLib/</identifier>
          <language>en</language>
          <relation> isPartOf: ISSN:4321-1234</relation>
          <rights>Copyright 2003 by the Division of More Fun Than Chemistry,
Inc., of the American Anything Else  Society.</rights>
          <subject>Elementary / Middle School</subject>
        </oai_dc>
      </metadata>
    </record>
    <record>
      <header>
        <identifier>oai:jce.divched.org:jcedlib-58</identifier>
        <datestamp>2003-06-03</datestamp>
      </header>
      <metadata>
        <oai_dc>
          <title>Journal of Watching Paint Dry</title>
          <creator>John W. Moore</creator>
          <creator>Jon L. Holmes</creator>
          <creator>Seymore Butz</creator>
          <creator>Willy Makit</creator>
          <creator>Betty Wohnt</creator>
          <description>Still better than Chemistry</description>
          <publisher>Fun Books, Inc.</publisher>
          <date>2003-01-01</date>
          <type>Monograph</type>
          <format>text/html</format>
          <identifier>http://jce.divched.org/JCEDLib/</identifier>
          <language>en</language>
          <relation> isPartOf: ISSN:1234-9876</relation>
          <rights>Copyright 2003 by Fun Books, Inc.</rights>
          <subject>Elementary / Middle School</subject>
        </oai_dc>
      </metadata>
    </record>
  </ListRecords>
</OAI-PMH>

I get the following HTML file:

<html>
   <body>
      <table>
         <tr>
            <td colspan="4">Type: Collection</td>
         </tr>
         <tr>
            <td>Betty Wohnt</td>
            <td>Journal of Chemical Education Digital Library</td>
            <td>2003-01-01</td>
            <td>The Division of Chemical Education, Inc., of the American
Chemical Society</td>
         </tr>
         <tr>
            <td>Betty Wohnt</td>
            <td>A Much More Fun Book</td>
            <td>2003-01-01</td>
            <td>The Division of More Fun Than Chemistry, Inc., of the
American Anything Else Society</td>
         </tr>
         <tr>
            <td>Dalt Wisney</td>
            <td>A Much More Fun Book</td>
            <td>2003-01-01</td>
            <td>The Division of More Fun Than Chemistry, Inc., of the
American Anything Else Society</td>
         </tr>
         <tr>
            <td>Edward W. Vitz</td>
            <td>Journal of Chemical Education Digital Library</td>
            <td>2003-01-01</td>
            <td>The Division of Chemical Education, Inc., of the American
Chemical Society</td>
         </tr>
         <tr>
            <td>John W. Moore</td>
            <td>Journal of Chemical Education Digital Library</td>
            <td>2003-01-01</td>
            <td>The Division of Chemical Education, Inc., of the American
Chemical Society</td>
         </tr>
         <tr>
            <td>Jon L. Holmes</td>
            <td>Journal of Chemical Education Digital Library</td>
            <td>2003-01-01</td>
            <td>The Division of Chemical Education, Inc., of the American
Chemical Society</td>
         </tr>
         <tr>
            <td>Theresa J. Zielinski</td>
            <td>Journal of Chemical Education Digital Library</td>
            <td>2003-01-01</td>
            <td>The Division of Chemical Education, Inc., of the American
Chemical Society</td>
         </tr>
         <tr>
            <td>Walt Disney</td>
            <td>A Much More Fun Book</td>
            <td>2003-01-01</td>
            <td>The Division of More Fun Than Chemistry, Inc., of the
American Anything Else Society</td>
         </tr>
         <tr>
            <td>William F. Coleman</td>
            <td>Journal of Chemical Education Digital Library</td>
            <td>2003-01-01</td>
            <td>The Division of Chemical Education, Inc., of the American
Chemical Society</td>
         </tr>
         <tr>
            <td colspan="4">Type: Monograph</td>
         </tr>
         <tr>
            <td>Betty Wohnt</td>
            <td>Journal of Watching Paint Dry</td>
            <td>2003-01-01</td>
            <td>Fun Books, Inc.</td>
         </tr>
         <tr>
            <td>John W. Moore</td>
            <td>Journal of Watching Paint Dry</td>
            <td>2003-01-01</td>
            <td>Fun Books, Inc.</td>
         </tr>
         <tr>
            <td>Jon L. Holmes</td>
            <td>Journal of Watching Paint Dry</td>
            <td>2003-01-01</td>
            <td>Fun Books, Inc.</td>
         </tr>
         <tr>
            <td>Seymore Butz</td>
            <td>Journal of Watching Paint Dry</td>
            <td>2003-01-01</td>
            <td>Fun Books, Inc.</td>
         </tr>
         <tr>
            <td>Willy Makit</td>
            <td>Journal of Watching Paint Dry</td>
            <td>2003-01-01</td>
            <td>Fun Books, Inc.</td>
         </tr>
      </table>
   </body>
</html>

Tested with Saxon 8.4 and Xalan-J 2.4.1

Jay Bryant
Bryant Communication Services

Current Thread