Summing BIll of Material quantities in XSLT

Faisal Shah

New Member
I am working on an XSLT report and need to find totals for components in a Bill of Materials. The input XML consists of Items and Links. For each Item, I need to get the quantity for each Link where it is used. To complicate matters, I need to go up the hierarchy and multiply the quantities by the parent quantities. The example only shows two levels, but it could be deeper than that. For example, Item assy1 uses 5 assy2 and 1 screw Item. Each Item assy2 uses 2 screws. So there would be a total of 5 assy2 and 11 screws (1 used by assy1 and each assy2 uses 2 (2X5)). I have figured out how to total the item quantities, but not how to multiply them up the hierarchy.Here is the source XML:\[code\]<items><item id="93516"><attrs><attr name="FILE_ID">assy1</attr></attrs></item><item id="93515"><attrs><attr name="FILE_ID">assy2</attr></attrs></item><item id="93514"><attrs><attr name="FILE_ID">screw</attr></attrs></item></items><links><link source="93516" destination="93514"><attrs><attr name="QUANTITY">5</attr></attrs></link><link source="93516" destination="93515"><attrs><attr name="QUANTITY">1</attr></attrs></link><link source="93515" destination="93514"><attrs><attr name="QUANTITY">2</attr></attrs></link></links>\[/code\]Here is some code I found and adapted to sum the quantities, but it doesn't multiply by the parent quantities:\[code\]<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/"> <xsl:apply-templates select="/items/item"/></xsl:template><xsl:template match="item"> <xsl:value-of select="attrs/attr[@name='FILE_ID']"/> - <xsl:variable name="item_id" select="@id"/> <xsl:call-template name="bomQty"> <xsl:with-param name="itemLinks" select="/links/link[@destination=$item_id]"/> </xsl:call-template> - </xsl:template><xsl:template name="bomQty"> <xsl:param name="itemLinks"/> <xsl:choose> <xsl:when test="$itemLinks"> <xsl:variable name="recursive_result"> <xsl:call-template name="bomQty"> <xsl:with-param name="itemLinks" select="$itemLinks[position() > 1]"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="number($itemLinks[1]/attrs/attr[@name='QUANTITY']) + $recursive_result"/> </xsl:when> <xsl:otherwise><xsl:value-of select="0"/></xsl:otherwise> </xsl:choose></xsl:template></xsl:stylesheet>\[/code\]Any help would be appreciated.
 
Back
Top