[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: job for xsl:key? (XSL 1.0 question)

Subject: Re: job for xsl:key? (XSL 1.0 question)
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Tue, 20 Feb 2007 22:49:34 +0530
Re:  job for xsl:key? (XSL 1.0 question)
Here is another possible solution (using node-set extension function):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                      xmlns:common="http://exslt.org/common"
                      exclude-result-prefixes="common"
                      version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:key name="by-type" match="contact" use="@type" />

<xsl:template match="/x">
  <totals>
    <xsl:variable name="rtf">
      <xsl:for-each select="contacts/contact[generate-id() =
generate-id(key('by-type', @type)[1])]">
       <temp><xsl:value-of select="count(key('by-type', @type)) *
../../types/type[@value = current()/@type]/@benchmark1" /></temp>
      </xsl:for-each>
    </xsl:variable>
    <benchmark val="{sum(common:node-set($rtf)/temp)}" />
  </totals>
</xsl:template>

</xsl:stylesheet>

When the above stylesheet is applied to the following XML:

<?xml version="1.0"?>
<x>
 <types>
    <type value="1" benchmark1="540" />
    <type value="2" benchmark1="640" />
    <type value="3" benchmark1="740" />
 </types>
 <contacts>
    <contact type="1" />
    <contact type="2" />
    <contact type="3" />
    <contact type="3" />
 </contacts>
</x>

The output produced is:

<?xml version="1.0" encoding="UTF-8"?>
<totals>
 <benchmark val="2660"/>
</totals>

On 2/20/07, Steve <stephen@xxxxxxxxx> wrote:
I could do the following easily by making a recursive template and
then looping through the contacts, and passing on the corresponding
benchmark value.  But could xsl:key make for a shorter, more elegant
solution?

Instead of performing an addition for each <contact>, could I simply
sum() them at once, somehow?

With the following XML

<types>
   <type value="1" benchmark1="540" />
   <type value="2" benchmark1="640" />
   <type value="3" benchmark1="740" />
</types>
<contacts>
   <contact type="1" />
   <contact type="2" />
   <contact type="3" />
   <contact type="3" />
</contacts>

desired output:

<totals>
   <benchmark val='2660'  />
</totals>


--
Regards,
Mukul Gandhi

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.