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

Subject: Re: [xsl] Sorting unique and sub-total problem
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Thu, 3 Jun 2004 09:10:18 -0700 (PDT)
Hi Roger,
  Please try the XSL -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xalan="http://xml.apache.org/xalan";>
<xsl:output method="text" version="1.0"
encoding="UTF-8" indent="yes"/>
	
<xsl:key name="by-card-number" match="ITEM"
use="CARD/@number"/>
	
<xsl:template match="/root">
  <xsl:variable name="rtf1">
    <xsl:for-each select="ITEM">
      <xsl:if test="generate-id(.) =
generate-id(key('by-card-number', CARD/@number)[1])">
	<group>
	  <number>
	    <xsl:value-of select="CARD/@number"/>
  	  </number>
	  <xsl:for-each select="key('by-card-number',
CARD/@number)">
	    <amount>
	      <xsl:value-of select="AMOUNT/@amt"/>
	    </amount>
	  </xsl:for-each>
	</group>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
		
  <xsl:variable name="rtf2">
    <xsl:for-each select="xalan:nodeset($rtf1)/group">
      <xsl:sort select="number" order="ascending"
data-type="number" />
        <xsl:copy-of select="." />
    </xsl:for-each>
  </xsl:variable>
		
  <xsl:for-each select="xalan:nodeset($rtf2)/group">
     Card: <xsl:value-of select="number" />  Total:
<xsl:value-of select="sum(amount)" />
  </xsl:for-each>

</xsl:template>
</xsl:stylesheet>

Regards,
Mukul

--- roger.wyatt@xxxxxxxxxxxxx wrote:
> 
> I am struggling with what is turning out to be a
> rather nasty problem using
> FOP  0.20.5 .  I have a set of unordered nodes along
> the lines of :...
> 
>       <ITEM item_id='1'>  <CARD number='123456789'
> \>  <AMOUNT amt='100'\>
> </ITEM>
>       <ITEM item_id='2'>  <CARD number='987654321'
> \>  <AMOUNT amt='30'\>
> </ITEM>
>       <ITEM item_id='3'>  <CARD number='123456789'
> \>  <AMOUNT amt='200'\>
> </ITEM>
>       <ITEM item_id='4'>  <CARD number='123456789'
> \>  <AMOUNT amt='50'\>
> </ITEM>
> 
> 
> 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.  I have proven this
> to myself using
> something along the lines of...
> 
> <xsl:for-each select=".//ITEM" >
> <xsl:sort select="CARD/@number" order="ascending" />
>       <fo:block>
>             CUR NUMBER: <xsl:value-of
> select=".//CARD/@number"/>
>             NXT NUMBER: <xsl:value-of
> select="following-sibling::
> */CARD/@number"/>
>             CUR ID IS: <xsl:value-of
> select="./@item_id"/>
>             NXT ID IS: <xsl:value-of
> select="following-sibling::
> */@item_id"/>
>       </fo:block>
> </xsl:for-each>
> 
> 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.
> 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.
> 
> Is there another approach I can take ?  Am I
> expecting too much from XSLT
> with all this... ?
> 
> Thanks for any help in advance...
> 
> 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



	
		
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 

Current Thread