Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - xsl totals composed from variables >Thread Next - Re: xsl totals composed from variables Re: xsl totals composed from variablesTo: 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>
>
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
