[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Help with multiple key grouping...


Subject: Re: [xsl] Help with multiple key grouping...
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 19 Nov 2007 17:28:38 -0500

At 2007-11-19 15:11 -0600, Hix, Robert W CIV NSWCDD, Q45 wrote:
I am really new to XSL and have run into a problem I can't get a handle
on.  I am developing a document tracking system that will help out with
letting employees know what documents are out for review, and when those
reviews are due.  I am also throwing in a list of all documents in the
library.  My current XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="gatorDocLibXsl_t3.xsl"?> <DocLib>
   <document>
...

I note you are using stylesheet association, as if you were browsing this document. That would imply that the version of XSLT you are using is 1.0 since browsers these days only support 1.0.


So far I have figured out how to group my output by category.  I also
need to group the documents within the category by title so that I can
sort them by version number.  Below is my current stylesheet for
grouping by category.

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

Here you indicate XSLT 2.0. Your answer would be a lot easier if you really could use 2.0, but so far I see you are only using 1.0 approaches to grouping.


I'll continue answering as if you need to use XSLT 1.0, but if you could use 2.0 it would be a lot easier.

Could someone please help me out with grouping both on category and
title?  I have looked through the archive postings most of the day and I
still cannot figure out how this works.  I am terribly confused and
cannot find a lot of information on this on the web.

You need to do grouping on a sub-document basis. When using XSLT 1.0, I typically do sub-document grouping using variables rather than using keys because keys are specified document-wide.


In the stylesheet below you'll see that within each category I put all of the documents of the category into a variable, and then I do the uniqueness grouping out of the variable instead of out of the key. This way the grouping acts on only a part of the document and not the entire document.

It can be done with keys, but involves standing on your hands and holding your tongue in a certain position. While the variable-based grouping method for XSLT 1.0 more easily accommodates both sub-document grouping and supra-document grouping.

And if you did have XSLT 2.0 available, your stylesheet would be a lot easier as it wouldn't need either keys or variables to do what you need.

I hope this helps.

. . . . . . . . . . . Ken

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

   <!-- Define keys for processing -->
   <xsl:key name="documentsByCategory" match="document" use="category"/>

   <!-- Main processing -->
   <xsl:template match="DocLib">
      <!-- Display all documents that are under review in a table-->
      <table border="1">
         <caption>Documents for Review</caption>
         <tr>
            <th>Document Title</th>
            <th>Version</th>
            <th>Due Date</th>
            <th>Adjudication Date</th>
            <th>Comment Template</th>
         </tr>

<xsl:for-each select="document">

            <xsl:sort select="substring(docReview/dueDate,7,4)"/> <!--
Sort Day -->
            <xsl:sort select="substring(docReview/dueDate,1,2)"/> <!--
Sort Day -->
            <xsl:sort select="substring(docReview/dueDate,4,2)"/> <!--
Sort Day -->

            <xsl:if test="docReview/complete='0'">
               <tr>
                  <td><xsl:value-of select="title"/></td>
                  <td><xsl:value-of select="version"/></td>
                  <td><xsl:value-of select="docReview/dueDate"/></td>
                  <td><xsl:value-of select="adjudication/date"/></td>
                  <td><xsl:value-of
select="docReview/commentTemplateFile"/></td>
               </tr>

            </xsl:if>
         </xsl:for-each>
      </table>

      <br/>
      <!-- Display all documents in the library in table format.  The
documents
           will be grouped by category (IRS, SSS, SRS, etc), then
grouped by
           document name, and then sorted by version number. -->

      <!-- This loop selects the unique categories and loops through
them
           iteratively making a table from each category -->
      <xsl:for-each select="document[count(. |
key('documentsByCategory', category)[1]) = 1]">
         <xsl:sort select="category"/>
            <br/>
            <table border ="1">
               <caption><xsl:value-of select="category"/>
Documents</caption>
               <tr>
                  <th>Title</th>
                  <th>Version</th>
                  <th>Review Comments/Response File</th>
               </tr>

               <!-- This loops through all of the documents in each
category -->
               <xsl:variable name="docs"
                             select="key('documentsByCategory',category)"/>
               <xsl:for-each select="$docs">
                  <xsl:sort select="title"/>
                  <xsl:if test="generate-id(.)=
                          generate-id($docs[title=current()/title][1])">
                    <tr>
                      <td><xsl:value-of select="title"/></td>
                      <td><xsl:value-of select="version"/></td>
                      <!-- <td><xsl:value-of
select="docReview/reviewCommentsFile"/></td> -->
                    </tr>
                  </xsl:if>
               </xsl:for-each>
            </table>

      </xsl:for-each>
   </xsl:template>

</xsl:stylesheet>


-- Comprehensive in-depth XSLT2/XSL-FO1.1 classes: Austin TX,Jan-2008 World-wide corporate, govt. & user group XML, XSL and UBL training RSS feeds: publicly-available developer resources and training G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread
Keywords