Re: [xsl] Sorting unique and sub-total problem

Subject: Re: [xsl] Sorting unique and sub-total problem
From: roger.wyatt@xxxxxxxxxxxxx
Date: Thu, 3 Jun 2004 19:01:18 +0100
Wendell,

Thanks a lot for the pointer.... I have now progressed some what, but I
still can see to get subtotals...
I stripped the XSL down so I can concentrate on the problem.  So now I
have....

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:key name="cards" match="ITEM" use="CARD/@number" />

<xsl:template match="/">
      <xsl:for-each select="/ITEMS" >
                  <xsl:call-template name="create-body" />
      </xsl:for-each>
 </xsl:template>

<xsl:template name="create-body" >
<BR/>
SELECTING UNIQUE USING A FOR-EACH
<xsl:for-each select="./ITEM[CARD/@number and
      generate-id(.) = generate-id(key('cards', CARD/@number))]" >

      <BR/>
      CARD:   <xsl:value-of select="./CARD/@number"/>
      NUMBER: <xsl:value-of select="count(key('cards', ./CARD/@number))" />

</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


The Raw XML is...

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<ITEMS>

      <ITEM item_id='1'>  <CARD number='123456789'/>  <AMOUNT amt='100'/>
</ITEM>
      <ITEM item_id='2'>  <CARD number='123456789'/>  <AMOUNT amt='200'/>
</ITEM>
      <ITEM item_id='3'>  <CARD number='987654321'/>  <AMOUNT amt='30'/>
</ITEM>
.     etc. etc.
</ITEMS>

I don't understand!!!

If I can count the nodes referenced by the key, why can't I sum the values
in those nodes.
I am going to read up on this tonight and may be I will crack it, but any
more pointers would be much appreciated...

Roger


***************************************
Roger Wyatt
Principal Consultant
Nomad Software Ltd.
***************************************
This e-mail (including attachments) is confidential and is intended solely
for the addressee.  Unless authorised you may not read, copy, use or store
this e-mail in any way, or permit others to do so.  If you have received it
in error, please contact Nomad Software on +44 (0) 20 7292 2400


                                                                                                                                       
                      Wendell Piez                                                                                                     
                      <wapiez@mulberryt        To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx                                               
                      ech.com>                 cc:                                                                                     
                                               Subject:  Re: [xsl] Sorting unique and sub-total problem                                
                      02/06/2004 16:48                                                                                                 
                      Please respond to                                                                                                
                      xsl-list                                                                                                         
                                                                                                                                       




Roger,

At 11:27 AM 6/2/2004, you wrote:
>The <ITEM> nodes can be in any order.  What I would like to get at the end
>is the following out put.
>
>       Card: 123456789  Total:350
>       Card: 987654321  Total:  30
>
>At the moment I can't even sort unique! I have tried to sort the nodes and
>then compare the current node to the following sibling, using...
>
><xsl:for-each select=".//ITEM" >
><xsl:sort select="CARD/@number" order="ascending" />
>       <xsl:if test="./CARD/@number[ not
>(.=following-sibling::ITEM/CARD/@number) ]">
>             ....print number etc. etc....
>
>But what I have found is that the following sibling is as it would be if
>the nodes have not been ordered.

Yes: the axes provide access to the document in its original order.

You have a fairly standard grouping problem. Grouping is a very common
requirement that is not addressed well by XSLT 1.0 (it is a species of
up-conversion), but for which a multitude of sophisticated techniques have
nonetheless been devised.

Start researching them on the FAQ at
http://www.dpawson.co.uk/xsl/sect2/sect21.html, and at Jeni's pages at
http://www.jenitennison.com/xslt/grouping.

>I fear that the only way to deal with this is to use XSLT to preorder the
>nodes and then use a 2nd script using the same approach as described.

You could do this, but your problem does not look so difficult that you
couldn't do it in one pass.

>Alternatively I could go right back to the process that originally created
>the XML file and order / sub total from there... something I really want
to
>avoid if at all possible.

This, however, is sometimes the best solution: sometimes the source comes
out of a relational database, and relational technology is very good at
this kind of operation.

>Is there another approach I can take ?  Am I expecting too much from XSLT
>with all this... ?

Nope. Just dig a little deeper under "grouping".

Good luck,
Wendell


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
   Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


--+------------------------------------------------------------------
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@xxxxxxxxxxxxxxxxxxxxxx>
--+--






Current Thread