xsl-list
[Top] [All Lists]

Re: XSL Query

2002-10-27 01:31:57
Hello Navin,

to make it short: It's a grouping problem and there is a nice technique called Muenchian Grouping (http://www.jenitennison.com/xslt/grouping/muenchian.xml).

The key you already got correctly:

<xsl:key name="locations" match="location" use="@country"/>

<xsl:template match="/">
<xsl:apply-templates select="//location[generate-id() = generate-id(key('locations', @country))]" mode="unique"/>
</xsl:template>

<xsl:template match="location" mode="unique">
<xsl:value-of select="@country"/>: <xsl:value-of select="sum(key('locations', @country)/sales)"/>
</xsl:template>

Regards,

Joerg


Mulberry Technologies List Owner wrote:
Date: Sat, 26 Oct 2002 19:51:51 -0400
From: Navin Kumar <navin1(_at_)umbc(_dot_)edu>
To: XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Need help on XSL query

Hello,

I need to display the sum of sales for each country only once! Current code is
not properly working and I understand it is because the loop itself is to
select all possible "@country" occurences but I also tried to use set:distinct
but am getting error like "set function not found in namespace...". Could
someone help me out in resolving this problem?  Here is the template I have
written:

===============================================
<?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="locations" match="location" use="@country" />
<xsl:template match="/">
  <html>
  <body>
  <table>
        <xsl:for-each select="//@country">
        <xsl:variable name="country">
                <xsl:value-of select="." />
        </xsl:variable>
        <xsl:variable name="Total">
                <xsl:value-of select="sum(key('locations',$country)/sales)" />
        </xsl:variable>
        <xsl:value-of select="$country" />
        <xsl:value-of select="$Total" />        <br/>
        </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

XML FILE IS
===========
<SalesDB>
 <time year="1996" quarter="1">
        <product category="Ford" brand="Escort">
                <location country="USA" state="NY">
                        <sales>70</sales>
                </location>
                <location country="USA" state="CA">
                        <sales>80</sales>
                </location>
                <location country="Canada" state="Ontario">
                        <sales>90</sales>
                </location>
                <location country="Canada" state="British Columbia">
                        <sales>100</sales>
                </location>
        </product>
      .........continue........
===============================================

Thanks and regards,
Navin


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



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