xsl-list
[Top] [All Lists]

Re: Sorting Question: Muenchian method? Substring in Key?

2002-09-27 10:40:52

--- Dimitre Novatchev <dnovatchev(_at_)yahoo(_dot_)com> wrote:

Hi Mark,

Yes, the use of the Muenchian method to solve your problem is very
straightforward:

source xml (the one provided by you, but reshuffled):
---------------------------------------------------
<topics>
  <topic>Chimpanzee</topic>
  <topic>Aardvark</topic>
  <topic>Beetle, Smaller European Elm Bark</topic>
  <topic>Anteater</topic>
  <topic>Beetle, American Burying</topic>
</topics>

stylesheet:
----------
<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 
  <xsl:output method="text"/>
  
  <xsl:variable name="NL" select="'
'"/>

  <xsl:key name="kTopic" match="topic" use="substring(.,1,1)"/>

  <xsl:template match="/">
    <xsl:for-each select="/*/topic
                              [
                               generate-id()
                              =
                               generate-id(key('kTopic',
                                               substring(.,1,1) 
                                               )[1]
                                           )
                              ]">
      <xsl:sort select="substring(.,1,1)"/>
      
      <xsl:value-of select="concat($NL, substring(.,1,1))"/>
      
      <xsl:for-each select="key('kTopic',substring(.,1,1))">
        <xsl:value-of select="concat($NL, .)"/>        
      </xsl:for-each>
      <xsl:value-of select="$NL"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Result:
-------

A
Aardvark
Anteater

B
Beetle, Smaller European Elm Bark
Beetle, American Burying

C
Chimpanzee



Just a minor addition: as they are at the moment, the results are not
sorted, but only grouped by their first letter.

Therefore, in the above code

      <xsl:for-each select="key('kTopic',substring(.,1,1))">
        <xsl:value-of select="concat($NL, .)"/>        
      </xsl:for-each>

should actually be:

       <xsl:for-each select="key('kTopic',substring(.,1,1))">
         <xsl:sort select="."/>
         <xsl:value-of select="concat($NL, .)"/>        
       </xsl:for-each>




=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list