Skip to content

Instantly share code, notes, and snippets.

@designermonkey
Created March 14, 2013 15:05
Show Gist options
  • Select an option

  • Save designermonkey/5162077 to your computer and use it in GitHub Desktop.

Select an option

Save designermonkey/5162077 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<data>
<site-basket-contents>
<group id="basket">
<item id="261" count="1">
<item id="price">135.00</item>
<item id="shipping">9.50</item>
</item>
<item id="57" count="1">
<item id="price">58.50</item>
<item id="shipping">9.50</item>
</item>
<item id="82" count="1">
<item id="price">100.50</item>
<item id="shipping">9.50</item>
</item>
</group>
</site-basket-contents>
</data>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="basket-contents" select="/data/site-basket-contents/group[@id = 'basket']/item"/>
<xsl:copy-of select="$basket-contents"/>
<xsl:call-template name="calculate">
<xsl:with-param name="items" select="$basket-contents"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="calculate">
<xsl:param name="items"/>
<xsl:param name="method" select="'price'"/>
<xsl:param name="position" select="1"/>
<xsl:param name="output" select="0"/>
<xsl:variable name="count" select="count($items)"/>
<xsl:choose>
<xsl:when test="$position &lt;= $count">
<xsl:variable name="this-item" select="$items[position() = $position]"/>
<xsl:variable name="price-sub" select="$this-item/item[@id = 'price'] * $this-item/@count"/>
<xsl:variable name="shipping-sub" select="$this-item/item[@id = 'shipping'] * $this-item/@count"/>
<xsl:call-template name="calculate">
<xsl:with-param name="items" select="$items"/>
<xsl:with-param name="method" select="$method"/>
<xsl:with-param name="position" select="$position + 1"/>
<xsl:with-param name="output">
<xsl:choose>
<xsl:when test="$method = 'price'"><xsl:value-of select="number($output) + number($price-sub)"/></xsl:when>
<xsl:when test="$method = 'shipping'"><xsl:value-of select="number($output) + number($shipping-sub)"/></xsl:when>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number($output, '###,###.00')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment