Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: xsl totals composed from variables

From: "Dimitre Novatchev" <dimitren@---.---.-->
To: NULL
Date: 11/3/2006 6:39:00 PM

Very unclear explanation -- try to provide a better one.

Cheers,
Dimitre Novatchev


"the_jos" <the_josb@y...> wrote in message 
news:1162571981.428004.188880@e......
> Dear reader,
>
> I am trying some things with xml/xsl and cannot find a solution for
> what I would like to do.
>
> I have 2 base items with name and price and two that are composed of
> base two (given name and quantity).
> The composition never changes, only the price can change.
> I can calculate the total value (price*quantity) with a for-each loop
> (using xsl:key) for each set of components, listing below.
>
> But now I want to calculate the total value of the components used in
> the composed items.
>
> I have looked into a 'loop' template, but this does not give the
> results I want.
> I tried various layouts, but only get a NaN value or the original value
> passed to the loop.
>
> I inserted the loop call right after I got the value p
> (price*quantity).
>
> Could someone point me to a working solution?
>
> Thanks
>
> Jos
>
>
> ------- loop template -----------
> <xsl:template name="loop">
> <xsl:param name="pr"/>
> <xsl:param name="t" select="0"/> <!-- not sure about this one -->
> <xsl:choose>
> <xsl:when test="$pr">
> <xsl:call-template name="loop">
> <xsl:with-param name="t" select="$pr+$t"/>
> </xsl:call-template>
> </xsl:when>
> <xsl:otherwise>
> <xsl:value-of select="$t" />
> </xsl:otherwise>
> </xsl:choose>
>
> </xsl:template>
>
>
> Call:
> <xsl:call-template name="loop">
>     <xsl:with-param name="pr" select="$p"/>
> </xsl:call-template>
>
>
>
> ---- working code -----
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>     version="1.0">
> <xsl:output method="xml" omit-xml-declaration="yes"/>
> <xsl:key name="comp" match="item" use="name" />
> <xsl:template match="/">
> <table border="1">
> <xsl:apply-templates/>
> </table>
> </xsl:template>
> <xsl:template match="item">
> <tr><td><xsl:value-of select="name" /> </td>
> <td><xsl:value-of select="price" /></td>
> <xsl:for-each select="component">
> <td><xsl:value-of select="cname" /></td>
> <td><xsl:value-of select="quantity" /></td>
> <xsl:variable name="cn" select="cname" />
> <xsl:variable name="q" select="quantity" />
>
> <xsl:for-each select="key('comp', $cn)">
> <xsl:variable name="p" select="price*$q" />
> <td><xsl:value-of select="$p"/></td>
> </xsl:for-each>
>
> <!-- <xsl:value-of select="quantity" /> -->
> </xsl:for-each>
> </tr>
> </xsl:template>
>
> </xsl:stylesheet>
>
> ---- Sample XML ----
>
> <items>
> <item>
>  <name>A</name>
>  <price>10</price>
> </item>
> <item>
>  <name>B</name>
>  <price>20</price>
> </item>
>  <item>
>  <name>C</name>
>  <price>10</price>
>  <component>
>   <cname>A</cname>
>   <quantity>2</quantity>
>   </component>
>   <component>
>   <cname>B</cname>
>   <quantity>5</quantity>
>   </component>
>  </item>
>  <item>
>  <name>D</name>
>  <price>10</price>
>  <component>
>   <cname>A</cname>
>   <quantity>2</quantity>
>   </component>
>   <component>
>   <cname>B</cname>
>   <quantity>5</quantity>
>   </component>
>  </item>
> </items>
> 




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent