Re: [xsl] Grouping question

Subject: Re: [xsl] Grouping question
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Thu, 12 Aug 2004 21:28:40 -0700 (PDT)
Hi Nishma,
  Please try this XSL -

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

<xsl:key name="by-date" match="temp/Line"
use="concat(substring(@pDate, 6, 2),'-',
substring(@pDate, 1, 4))" /> 
 
<xsl:template match="/Statements">
  <xsl:for-each select="Statement">
    Statement: <xsl:value-of select="@number"
/><xsl:text>&#xa;</xsl:text>
    Name: <xsl:value-of select="Property/@name"
/><xsl:text>&#xa;</xsl:text>
    <xsl:variable name="rtf">
      <temp>
        <xsl:copy-of select="Property/Line" />
      </temp> 
    </xsl:variable>
    <xsl:for-each
select="xalan:nodeset($rtf)/temp/Line">
      <xsl:if test="generate-id(.) =
generate-id(key('by-date', concat(substring(@pDate, 6,
2),'-', substring(@pDate, 1, 4)))[1])">
        <xsl:for-each select="key('by-date',
concat(substring(@pDate, 6, 2),'-', substring(@pDate,
1, 4)))">
          Date <xsl:value-of select="@pDate" />
amount: <xsl:value-of select="@amount"
/><xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
        MONTH TOTAL : <xsl:value-of
select="sum(key('by-date', concat(substring(@pDate, 6,
2),'-', substring(@pDate, 1, 4)))/@amount)"
/><xsl:text>&#xa;</xsl:text>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each>           
</xsl:template>
  
</xsl:stylesheet>

Regards,
Mukul

--- Nishma Jain <njain@xxxxxxxxxxxx> wrote:

> Hi, 
> 
> I'm having a grouping issue. In my case root tag is
> "Statements" and
> under Statements I can have multiple Statement tags.
> I'm using muenchian
> method for grouping but I need to group within a
> Statement tag as oppose
> to the whole xsl doc. Now within each Statement I
> have to calculate
> month totals. Since all the months and dates are
> dynamic so I'm using
> grouping to get a set of amounts according to the
> month and year. As you
> can see in the output, amounts from the 2nd
> statement node are showing
> up in the first statement since key is getting
> applied to the whole xml
> doc. I need to restrict it to the current statement
> node only. Is there
> any way to do it? How can I specify current id or
> any other attribute of
> the statement in the key or any other idea???
> 
> Hope I made myself clear. Attached are my test xml
> and xsl scripts. 
> 
> Thanks in advance,
> Nishma
> 
> My output...
> 
> Statement:1
> 	Name:AAA
>     	Date 2004-07-09 amount: 1.00
>     	Date 2004-07-10 amount: 2.00
>     	Date 2004-07-08 amount: 1.00
>     	Date 2004-07-19 amount: 3.00
>      MONTH TOTAL : 7.00
>     	Date 2004-08-09 amount: 3.00
>     	Date 2004-08-10 amount: 4.00
>     	Date 2004-08-11 amount: 5.00
>     	Date 2004-08-22 amount: 2.00
>      MONTH TOTAL : 14.00
> Statement:2
> 	Name:BBB
>     	Date 2004-09-10 amount: 4.00
>      MONTH TOTAL : 4.00
>      
>   Required output .....
>   
> Statement:1
> 	Name:AAA
>     	Date 2004-07-09 amount: 1.00
>     	Date 2004-07-10 amount: 2.00
>      MONTH TOTAL : 3.00
>     	Date 2004-08-09 amount: 3.00
>     	Date 2004-08-10 amount: 4.00
>     	Date 2004-08-11 amount: 5.00
>      MONTH TOTAL : 12.00
> Statement:2
> 	Name:BBB
> 		Date 2004-07-08 amount: 1.00
>     	Date 2004-07-19 amount: 3.00
>      MONTH TOTAL : 4.00
>      	Date 2004-08-22 amount: 2.00
>      MONTH TOTAL : 2.00
>     	Date 2004-09-10 amount: 4.00
>      MONTH TOTAL : 4.00
> 
> My xml:
> 
> <Statements>
>  	<Statement number="1" total="15">
>  		<Property name="AAA">
>  			<Line pDate="2004-07-09" amount="1.00"/>
>  			<Line pDate="2004-07-10" amount="2.00"/>
>  			<Line pDate="2004-08-09" amount="3.00"/>
>  			<Line pDate="2004-08-10" amount="4.00"/>
>  			<Line pDate="2004-08-11" amount="5.00"/>
>   		</Property>
>  	</Statement>
>  	<Statement number="2" total="10">
>  		<Property name="BBB">
>  			<Line pDate="2004-07-08" amount="1.00"/>
>  			<Line pDate="2004-08-22" amount="2.00"/>
>  			<Line pDate="2004-07-19" amount="3.00"/>
>  			<Line pDate="2004-09-10" amount="4.00"/>
>   		</Property>
>  	</Statement>
> </Statements>
> 
> My xsl:
> 
> <?xml version="1.0" encoding="iso-8859-1"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> <xsl:key name="line-date"
> match="Statements/Statement/Property/Line"
> use="concat(substring(@pDate, 6, 2),'-',
> substring(@pDate, 1, 4))" />
> 
> <xsl:template match="Statements">
> 		<xsl:apply-templates  select='Statement'/>
> </xsl:template>
> 
> <xsl:template match="Statement">
> Statement:<xsl:value-of select='@number'/>
> 		<xsl:apply-templates select='Property'/> 
> 		
> </xsl:template>
> 
> <xsl:template match="Property">
> 	Name:<xsl:value-of select='@name'/>
> 	<xsl:apply-templates select="Line[generate-id(.) =
> generate-id(key('line-date',
> concat(substring(@pDate, 6, 2),'-',
> substring(@pDate, 1, 4)) )[1]) ]"/>
> </xsl:template>
> 
> <xsl:template match="Line">
> 	<xsl:for-each select="key('line-date',
> concat(substring(@pDate,
> 6, 2),'-', substring(@pDate, 1, 4)))">
>     	Date <xsl:value-of select='@pDate'/> amount:
> <xsl:value-of
> select='format-number(@amount, "#,###,##0.00")'/>
>     </xsl:for-each>
>      <!--Month totals.. -->MONTH TOTAL :
> <xsl:value-of
>
select="format-number(sum(key('line-date',concat(substring(@pDate,
> 6,
> 2),'-', substring(@pDate, 1,
> 4)))/@amount),'#,###,##0.00')"/>
> </xsl:template>							
> 
> </xsl:stylesheet>



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail

Current Thread