Re: [xslt] using key() function with key name in a variable



>   Seems very easy to fix to follow the common behaviour, so it's 
probably not
> worth starting a standard reading contest over xsl-list :-)
>   Patch enclosed, untested since you didn't provided a full example.

Thanks very much -- I can confirm it works.  Sorry about the lack of an 
example, but maybe better late than never:

source doc: --------------------
<root>
  <list type="one"/>
  <list type="two"/>
  <entries>
    <entry cat1="a" cat2="a" qty="1"/>
    <entry cat1="a" cat2="b" qty="2"/>
    <entry cat1="b" cat2="b" qty="3"/>
  </entries>
</root>

stylesheet: -------------------
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    version="1.0">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
  <result>
    <xsl:apply-templates/>
  </result>
</xsl:template>

<xsl:key name="k1" match="entry" use="@cat1"/>
<xsl:key name="k2" match="entry" use="@cat2"/>

<xsl:template match="list">
  <xsl:variable name="k">
    <xsl:choose>
      <xsl:when test="@type='one'">k1</xsl:when>
      <xsl:otherwise>k2</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <total>
    <type><xsl:value-of select="@type"/></type>
    <a><xsl:value-of select="sum( key($k,'a')/@qty )"/></a>
    <b><xsl:value-of select="sum( key($k,'b')/@qty )"/></b>
  </total>
</xsl:template>
<xsl:template match="entries"/>
</xsl:stylesheet>

output: -----------------------
<?xml version="1.0"?>
<result>
  <total><type>one</type><a>3</a><b>3</b></total>
  <total><type>two</type><a>1</a><b>5</b></total>

</result>

Thanks again, John Escott.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]