[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Sorting Question: Muenchian method? Substring in K

Subject: Re: Sorting Question: Muenchian method? Substring in Key?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Fri, 27 Sep 2002 08:53:45 -0700 (PDT)
aardvark anteater
--- Thomas_M <Thomas_M at bls dot gov> wrote:

 
> Hello. This is my first XSLT project. I'm having trouble with
> grouping
> sorted items (seems to be a common theme). I have a XML file like
> this
> (simplified):
> 
> <topics>
>   <topic>Anteater</topic>
>   <topic>Aardvark</topic>
>   <topic>Beetle, Smaller European Elm Bark</topic>
>   <topic>Beetle, American Burying</topic>
>   <topic>Chimpanzee</topic>
> </topics>
> 
> I would like the output to be sorted and grouped, with each group
> getting a
> heading:
> 
> A
> Aardvark
> Anteater
> 
> B
> Beetle, American Burying
> Beetle, Smaller European Elm Bark
> 
> C
> Chimpanzee
> 
> The corresponding part of my XSL is below.
> 
>   <xsl:template match="topics">
>    <table border="1">
>       <xsl:for-each select="topic">
>         <xsl:sort select="."/>
>         <xsl:variable name="previndex" select="position()-1"/>
>         <xsl:if test="not(substring(.,1,1)
>                       = substring(/topics/topic[$previndex],1,1))">
> 		<tr><th><xsl:value-of select="substring(.,1,1)"/></th></tr>
> 
>         </xsl:if>
>             <tr>
>               <td><xsl:value-of select="."/></td>
>             </tr>
>       </xsl:for-each>
>     </table>
>   </xsl:template> 
> 
> As you can see, I'm comparing an element in a sorted node-set with a
> document-order node-set. This works only if the XML is pre-sorted. Is
> there
> a way to create the sorted node-set first, so that I can both
> traverse
> and
> use it in my test?
> 
> I have seen a few examples of the Muenchian method, and it would seem
> this
> is another (perhaps better) way solve my problem. However, I've tried
> several times to implement it and can't seem to get it right (can you
> use a
> substring in a key?)
> 
> Thanks for any insight,
> 
> - Mark.


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="'&#xA;'"/>

  <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


Hope this helped.





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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.