xsl-list
[Top] [All Lists]

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

2002-09-30 07:35:14
Thanks to all who responded. I understand Muenchian grouping much better
now. I think of <xsl:key> as creating a hash table, and generate-id() like a
kind of checksum() routine.

My remaining question is: how efficient is it? I assume the <xsl:key> is
fairly efficient because the pregenerated lookup table makes subsequent
lookups quick. However, the Muenchian grouping in my simple example resulted
in no less than five calls to substring(), four of which are inside one or
more levels of <for-each> iteration.

-- 
Mark Thomas                       |_|
Internet Systems Architect        -+-
User Technology Associates, Inc.   |
Thomas_M(_at_)bls(_dot_)gov                  /-\


-----Original Message-----
From: Michael Kay [mailto:michael(_dot_)h(_dot_)kay(_at_)ntlworld(_dot_)com] 
Sent: Friday, September 27, 2002 5:05 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Sorting Question: Muenchian method? 
Substring in Key?


There are two main approaches to grouping:

(a) the "not(x = preceding::x)" method
(b) the Muenchian method

Method (a) works only where the grouping key is the 
string-value of a node; this is because it relies on the 
special properties of "=" when comparing two node-sets. Where 
the grouping key is anything else (e.g. the first character 
of the string value), you need to use Muenchian grouping.

Michael Kay
Software AG
home: Michael(_dot_)H(_dot_)Kay(_at_)ntlworld(_dot_)com
work: Michael(_dot_)Kay(_at_)softwareag(_dot_)com 

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf 
Of Thomas_M
Sent: 27 September 2002 16:27
To: 'xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com'
Subject: [xsl] Sorting Question: Muenchian method? Substring in Key?


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.

-- 
Mark Thomas                       |_|
Internet Systems Architect        -+-
User Technology Associates, Inc.   |
Thomas_M(_at_)bls(_dot_)gov                  /-\




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



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



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