xsl-list
[Top] [All Lists]

RE: Please help with Grouping & sorting ! ;-(

2002-09-04 07:32:33
This was the PERFECT solution!... I did have a look at the Muench method,
but my head started to spin! ;-)

-----Original Message-----
From: Helder da Rocha [mailto:helder(_at_)argonavis(_dot_)com(_dot_)br]
Sent: 03 September 2002 18:23
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Please help with Grouping & sorting ! ;-(


Hi Amir,

You can try a different approach using keys, and avoid the duplicate
categories:

<xsl:key name="cat key" match="item" use="@category" />

<xsl:template match="catalogue">
     <dl>
        <xsl:for-each select="item[generate-id(.) =
generate-id(key('cat key', @category))]">
            <xsl:sort select="@category" />
            <xsl:for-each select="key('cat key', @category)">
                <xsl:sort select="." />
                <xsl:if test="position() = 1">
                    <dt><xsl:value-of select="@category" /></dt>
                </xsl:if>
                <dd><xsl:value-of select="." /></dd>
            </xsl:for-each>
        </xsl:for-each>
     </dl>
</xsl:template>

The result is:
  gold
  this is a bracelet
  this is a ring
  platinum
  this is a platinum time piece
  Silver
  this is a silver watch
Which is what you want (except, probably, putting the first letter in
upper-case, but that you can accomplish with translate()).

I learned this approach with Doug Tidwell in his great book "XSLT", by
O'Reilly. He calls it the "Muench method" (named after the person who
suggested it).

Helder.

--
Helder da Rocha (helder(_at_)argonavis(_dot_)com(_dot_)br)
Web Consultant
www.argonavis.com.br
São Paulo, Brazil
+55 (11) 9291 0567

----- Original Message -----
From: "Khan, Amir" <amkhan(_at_)lehman(_dot_)com>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Cc: "Khan, Amir" <amkhan(_at_)lehman(_dot_)com>
Sent: Tuesday, September 03, 2002 1:26 PM
Subject: [xsl] Please help with Grouping & sorting ! ;-(


Hi,

I have some XML of the form

<catalogue>
<item price="1" category="gold">this is a bracelet</item>
<item price="5" category="Silver">this is a silver watch</item>
<item price="1" category="gold">this is a ring</item>
<item price="0" category="platinum">this is a platinum time
piece</item>
</catalogue>


What I want to do is make a webpage like :-

Platinum
this is a platinum timepiece
Gold
* this is a bracelet
* this is a ring
Silver
* this is a silver watch


I've managed to group & sort the data. My problem is detecting when a new
category appears and then spitting out the label as a Heading.

I have something like :-

  <xsl:variable name="previousCategory">XXX</xsl:variable>
  <xsl:for-each select="item">
        <xsl:sort select="@price" data-type="number"
                  order="ascending"/>
          <!-- If the category has changed then this is a new
               "heading"
          -->
          <xsl:if test="@category[.!=$previousCategory]">
<xsl:if test="@category[.!="XXX"]">
<!-- no more than a </UL> -->
                <xsl:call-template name="end of group"/>
            </xsl:if>
<!-- new group = spit out new heading
no more than a <UL> followed by the @category
-->
            <xsl:call-template name="new group"/>
          </xsl:if>
  <xsl:call-template name="item element"/>
          <!-- Store this Category for the next iteration
   so we know if we should display a new heading
     -->
          <xsl:variable name="previousCategory" select="@category"/>
    </xsl:for-each>

    <xsl:template name="item element">
    <LI>
        <xsl:apply-templates/>
    </LI>
    </xsl:template>


What I actually see is :-

Platinum
this is a platinum timepiece
Gold
* this is a bracelet
Gold
* this is a ring
Silver
* this is a silver watch

Its ordered correctly BUT it does'nt realise that its already displayed a
heading (eg Gold) and its not supposed to show it again until it changes
to
Silver. I think its because the variable previousCategory is write-once
but
then how do I keep track of the previously displayed attribute "category"
in
the previous sibling in the SORTED node set?

Help! ;-((

Amir

--------------------------------------------------------------------------
----
This message is intended only for the personal and confidential use of the
designated recipient(s) named above.  If you are not the intended recipient
of this message you are hereby notified that any review, dissemination,
distribution or copying of this message is strictly prohibited.  This
communication is for information purposes only and should not be regarded as
an offer to sell or as a solicitation of an offer to buy any financial
product, an official confirmation of any transaction, or as an official
statement of Lehman Brothers.  Email transmission cannot be guaranteed to be
secure or error-free.  Therefore, we do not represent that this information
is complete or accurate and it should not be relied upon as such.  All
information is subject to change without notice.



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




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


------------------------------------------------------------------------------
This message is intended only for the personal and confidential use of the 
designated recipient(s) named above.  If you are not the intended recipient of 
this message you are hereby notified that any review, dissemination, 
distribution or copying of this message is strictly prohibited.  This 
communication is for information purposes only and should not be regarded as an 
offer to sell or as a solicitation of an offer to buy any financial product, an 
official confirmation of any transaction, or as an official statement of Lehman 
Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  
Therefore, we do not represent that this information is complete or accurate 
and it should not be relied upon as such.  All information is subject to change 
without notice.



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



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