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

RE: [xsl] recursive group-by possible?


Subject: RE: [xsl] recursive group-by possible?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 19 Jul 2007 13:51:28 +0100

Sure, recursive grouping is possible (and I come across it quite
frequently). For an example see

http://www.idealliance.org/proceedings/xml04/papers/111/mhk-paper.html

section 3.2 (though you may need earlier sections to understand the context)

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Torsten Scha_an [mailto:schassan@xxxxxx]
> Sent: 19 July 2007 13:45
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] recursive group-by possible?
>
> Dear colleagues,
>
> I want to transform mets/structMap entries into an unordered
> list. I need to transform my xslt1-thinking into xslt2-code.
> Is there a better solution (recursion?) than the one below?
> Which BTW is working, but has got the problem, that the
> number of levels is unlimited, but my stylesheet isn't.
> Separator would always be ' - '.
>
> Thanks in advance, best, Torsten
>
> Input:
>
> <mets>
> <structMap TYPE="physical" LABEL="Book - Facsimile">
>    <div TYPE="facsimile" LABEL="Book"></div></structMap>
> <structMap TYPE="physical" LABEL="Book - Reconstruction - Text1">
>    <div TYPE="reconstruction" LABEL="Book -
> Text1"></div></structMap> <structMap TYPE="physical"
> LABEL="Book - Reconstruction - Palimpsest - Text2">
>    <div TYPE="reconstruction" LABEL="Book - Palimpsest -
> Text2"></div></structMap> <structMap TYPE="physical"
> LABEL="Book - Reconstruction - Palimpsest - Text3">
>    <div TYPE="reconstruction" LABEL="Book - Palimpsest -
> Text3"></div></structMap> </mets>
>
>
> Desired output:
> <ul>
>    <li>physical        <-- from @TYPE -->
>    <ul>
>       <li>Facsimile</li>    <-- from @LABEL -->
>       <li>Reconstruction   <-- from @TYPE -->
>          <ul>
>             <li>Book1</li>   <-- from @LABEL -->
>             <li>Palimpsest
>               <ul>
>                  <li>Book2</li>
>                  <li>Book3</li>
>               </ul>
>            </li>
>         </ul>
>      </ul>
>     </li>
> </ul>
>
>
> xsl:
>
> <xsl:template match="mets:mets">
>  <ul>
>   <xsl:for-each-group select="mets:structMap" group-by="@TYPE">
>    <li><a href="#"><xsl:value-of select="@TYPE" />
>     <ul>
>      <xsl:for-each-group select="current-group()"
> group-by="mets:div/@TYPE">
>       <li><a href="#"><xsl:value-of select="mets:div/@TYPE" />
>        <ul>
>         <xsl:for-each-group
>           select="current-group()"
>           group-by="if
> (contains(substring-after(mets:div/@LABEL,' - '),' - '))
>                then
> substring-before(substring-after(mets:div/@LABEL,' - '),' - ')
>                else substring-after(mets:div/@LABEL,' - ')">
>          <xsl:choose>
>           <xsl:when
> test="contains(substring-after(mets:div/@LABEL,' - '),' - ')">
>            <li>
>             <xsl:value-of
> select="substring-before(substring-after(mets:div/@LABEL,' -
> '),' - ')" />
>             <ul>
>              <xsl:for-each-group
>                select="current-group()"
>                group-by="if
> (contains(substring-after(substring-after(mets:div/@LABEL,' -
> '),' - '),' - '))
>                     then
> substring-before(substring-after(substring-after(mets:div/@LAB
> EL,' - '),' - '),' - ')
>                     else
> substring-after(substring-after(mets:div/@LABEL,' - '),' - ')">
>                <xsl:sort
> select="substring-after(substring-after(mets:div/@LABEL,' -
> '),' - ')"/>
>                <li><xsl:value-of
> select="substring-after(substring-after(mets:div/@LABEL,' -
> '),' - ')"
> /></li>
>              </xsl:for-each-group>
>             </ul>
>            </li>
>           </xsl:when>
>           <xsl:otherwise><xsl:value-of
> select="substring-after(mets:div/@LABEL,' - ')" /></xsl:otherwise>
>          </xsl:choose>
>         </xsl:for-each-group>
>        </ul>
>       </a></li>
>      </xsl:for-each-group>
>     </ul>
>    </a></li>
>   </xsl:for-each-group>
>  </ul>
> </xsl:template>
>
>
>
> --
> Torsten Schassan
> Herzog August Bibliothek, Postfach 1364, D-38299 Wolfenbuettel
> Tel.: +49-5331-808-117, schassan {at} hab.de
> http://www.hab.de; http://www.hab.de/forschung/projekte/weiss64.htm
>
> Projekt CESG (Codices Electronici Sangallenses)
>    http://www.cesg.unifr.ch


Current Thread