xsl-list
[Top] [All Lists]

Re: [xsl] Multi-grouping with keys (back of book index)

2006-10-21 12:27:01
This is XSLT 2.0, so it is much more easier -- in particular one
doesn't need to use keys and the Muenchian method:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>
<xsl:output method="text"/>

<xsl:template match="/*">
  <xsl:for-each-group select="indexterm"
    group-by="substring(@name,1,1)">
    <xsl:sort select="current-grouping-key()"/>
    <xsl:text>&#xA;&#xA;</xsl:text>

    <xsl:value-of select="current-grouping-key()"/>

    <xsl:for-each-group select="current-group()"
     group-by="@name">
     <xsl:sort select="current-grouping-key()"/>

     <xsl:text>&#xA;</xsl:text>
     <xsl:value-of select="current-grouping-key()"/>

     <xsl:for-each select="current-group()[normalize-space()]">
       <xsl:value-of select="concat('&#xA;-',.)"/>
     </xsl:for-each>
    </xsl:for-each-group>
  </xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>


--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play




On 10/21/06, philip vallone <philip(_dot_)vallone(_at_)verizon(_dot_)net> wrote:
Hi, I am building a back of the book index using keys and grouping. I am
able to group the <indexterms> to the @name attribute, but I can't figure
out how to group the @name attribute to its respective letter. My desired
output is:

Desired output:

E
E190
-Aircraft Tail
-Landing Gear
Election
-voting

G
GMM
-Aircraft Logbook Entry
-MX-4 Deffered Items

S
Smith Airways
-MEL Policy and Revision number




Here is my XML:


<book>
<indexterm name="GMM">Aircraft Logbook Entry</indexterm>
<indexterm name="Smith Airways">MEL Policy and Revision number</indexterm>
<indexterm name="GMM">MX-4 Deffered Items</indexterm>
<indexterm name="E190">Aircraft Tail</indexterm>
<indexterm name="E190">Landing Gear</indexterm>
<indexterm name="Election">Voting</indexterm>
</book>

Here is my XSL 2.0:

<xsl:output method="html"/>
       <xsl:variable name="XML1" select="/"/>
       <xsl:key name="list" match="//indexterm" use="@name"/>
       <xsl:key name="letter" match="//indexterm" use="substring(@name,
1,1)"/>
       <xsl:template match="/">
               <html>
                       <head>
                               <title>Manual Index</title>
                       </head>
                       <style type="text/css">
h2 {font-family:verdana,helvetica,sans-serif;font-size:13pt}
li {font-family:verdana,helvetica,sans-serif;font-size:11pt}
</style>
                       <body>
                               <xsl:for-each select="$XML1">
                                       <xsl:for-each
select="//indexterm[generate-id(.)=generate-id(key('list', @name))]/@name">
                                               <xsl:sort/>
                                               <xsl:variable name="key"
select="translate(substring(.,1,1),
                                 'abcdefghijklmnopqrstuvwxyz',
                                 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
                                               <xsl:value-of
select="$key"/>
                                               <xsl:for-each
select="//indexterm[generate-id(.)=generate-id(key('list', @name))]/@name">
                                                       <xsl:sort/>
                                                       <xsl:variable
name="get" select="../@name"/>
                                                       <h2>

<xsl:value-of select="."/>
                                                       </h2>
                                                       <ul>

<xsl:for-each select="key('list', $get)">

<xsl:sort/>
                                                                       <li>

<xsl:value-of select="."/>

</li>

</xsl:for-each>
                                                       </ul>
                                               </xsl:for-each>
                                       </xsl:for-each>
                               </xsl:for-each>
                       </body>
               </html>
       </xsl:template>



Thanks for the help.

Phil



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--