T
the_jos
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
aram name="pr"/>
<xsl
aram 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
therwise>
<xsl:value-of select="$t" />
</xsl
therwise>
</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
utput 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>
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
<xsl
<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
<xsl:value-of select="$t" />
</xsl
</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
<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>