xsl-list
[Top] [All Lists]

RE: xslt 2.0 grouping; was Dividing a list of nodes into parts of the alphabet

2004-12-03 03:33:40
 

    -----Original Message-----
    From: Michael Kay
    
    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.
 


I found this intriguing... and not exactly clear (to me at least).
 Test input

<?xml version="1.0" encoding="UTF-8"?>
 <categories>
   <item>
     <artist>Sbout the Moon</artist>
     <title>Carved in Stigmata Wounds</title> 
   </item>
   <item>
     <artist>Satyricon</artist>
     <title>Rebel Extravaganza</title>
   </item>
   <item>
     <artist>Secrets of the Moon</artist>
     <title>Carved in Stigmata Wounds</title> 
   </item>
 </categories>
 

stylesheet:

  <xsl:template match="/">
         <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="group"/>

  <xsl:template match="categories">
    <xsl:for-each-group select="item" group-by="f:grouping-key(.)">
      <xsl:value-of select="substring(artist,1,1)"/>
    </xsl:for-each-group>
    <xsl:apply-templates select="item"/>
  </xsl:template>

  <xsl:template match="item">
      <xsl:message>
      <xsl:value-of select="f:grouping-key(.)[1]"/>
    </xsl:message>
  </xsl:template>


<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>


Comments:

1. If the item/artist are not lexically sequential it bombs.
2. I don't understand the grouping, hence the item template,
   which shows outptu I don't understand either.

Anyone care to explain further please?

TIA daveP.

-- 
DISCLAIMER:

NOTICE: The information contained in this email and any attachments is 
confidential and may be privileged.  If you are not the intended 
recipient you should not use, disclose, distribute or copy any of the 
content of it or of any attachment; you are requested to notify the 
sender immediately of your receipt of the email and then to delete it 
and any attachments from your system.

RNIB endeavours to ensure that emails and any attachments generated by
its staff are free from viruses or other contaminants.  However, it 
cannot accept any responsibility for any  such which are transmitted.
We therefore recommend you scan all attachments.

Please note that the statements and views expressed in this email and 
any attachments are those of the author and do not necessarily represent
those of RNIB.

RNIB Registered Charity Number: 226227

Website: http://www.rnib.org.uk




--~------------------------------------------------------------------
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>
--~--



<Prev in Thread] Current Thread [Next in Thread>