RE: [xsl] Dividing a list of nodes into parts of the alphabet

Subject: RE: [xsl] Dividing a list of nodes into parts of the alphabet
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 3 Dec 2004 09:26:59 -0000
Since you're using XSLT 2.0 I would assume that you are doing the grouping
using xsl:for-each-group. A useful technique here is to use a function to
calculate the grouping key:

<xsl:for-each-group select="item" group-by="f:grouping-key(.)">
  <xsl:result-document ...

  </xsl:result-document>
</xsl:for-each-group>


<xsl:function name="f:grouping-key" as="xs:string">
  <xsl:param name="item" as="element(item)"/>
  <xsl:variable name="ranges">
    <range from="A" to="G"/>
    <range from="H" to="L"/>
    <range from="M" to "P"/>
    <range from="Q" to "Z"/>
  </xsl:variable>
  <xsl:sequence select="$ranges/range 
     [string(@from) lt substring($item/artist,1,1) and 
      string(@to) ge substring($item/artist,1,1)]/@from"/>
</xsl:function>

I hope this gives you some ideas about how to tackle the problem. You could
of course read the table of ranges from a secondary input document to make
it more flexible, or you could calculate it as a function of the input data.

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

> -----Original Message-----
> From: Jannis Pohlmann [mailto:info@xxxxxxxxxxx] 
> Sent: 03 December 2004 04:36
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Dividing a list of nodes into parts of the alphabet
> 
> Hello,
> 
> I am working on a shop system for a friend of mine. This 
> system shall parse
> one XML file into several static XHTML documents.
> As you may expect, there are several problems to manage. 
> XHTML documents
> have to be created for each product, for each category and so on.
> Creating a page for each product in its categories
> directory - e.g. /catalogue/CDs/product_120.html - is easy using
> xsl:result-document since I make use of the latest version of Saxon
> and XSLT 2.0.
> 
> But with the categories I ran into several logistic yet highly
> interesting problems:
> 
> To explain this I will have describe some parts of my XML structure:
> 
> ---
> 
> <categories>
>     <group name="CDs">
>        <cat id="cd">CD</cat>
>        <cat id="mcd">MCD</cat>
>        <cat id="digicd">Digipack CD</cat>
>     </group>
>     <group name="LPs">
>        <cat id="lp">LP</cat>
>        <cat id="ep">EP</cat>
>        <cat id="10">10 inch</cat>
>     </group>
>     <group name="Textiles">
>        <cat id="tshirt">T-Shirt</cat>
>        <cat id="ls">Longsleeve</cat>
>     </group>
> </categories>
> 
> (...)
> 
> <item>
>     <artist>Satyricon</artist>
>     <title>Rebel Extravaganza</title>
>     (...)
> </item>
> <item>
>     <artist>Secrets of the Moon</artist>
>     <title>Carved in Stigmata Wounds</title>
> </item>
> 
> ---
> 
> As you may imagine, the shop might have lots more
> CDs than, for example, Textiles, so I would like to add an
> opportunity to split the page created for each
> <group> into one or more pages, each including all artists beginning
> with the same letter or one letter out of a range of letters.
> 
> ---
> 
> First example:
>     (catalogue/CDs/CDs.html -> mapped to A.html by Apache's 
> mod_rewrite)
>     catalogue/CDs/A.html
>     catalogue/CDs/B.html
>     (...)
>     catalogue/CDs/Z.html
>     (and catalogue/CDs/0-9.html of course but that should be no
>     problem with  <xsl:sequence select="0 to 9"> or somewhat)
> 
> Second example:
>     (catalogue/LPs/LPs.html -> redirected to A-E.html by Apache's 
> mod_rewrite)
>     catalogue/LPs/A-E.html
>     catalogue/LPs/F-J.html
>     (...)
>     catalogue/LPs/U-Z.html
> 
> Third example
>     catalogue/Textiles/Textiles.html
> 
> ---
> 
> I think this is kind of tricky as I want to make the splitting option
> as variable as possibly.
> What I don't know is if I need a new attribute or child in 
> <group/> with
> comma-separated letters/letter-groups and how I may interpret 
> these. Or
> is it possible to interpret letters or letter-groups as xsl:sequence's
> in order to iterate through them?
> 
> I have been thinking about this for two hours now. Perhaps some of
> you do know a solution for it or at least are able to help in 
> developing
> one.
> 
> Greetings,
> Jannis

Current Thread