I am new to XSL so I don´t really know how to do this. I have a for-each statement that does some computations for each element of a type "cell". How can I sum-up the results and store them in a variable so I can display it? I have included a part of the code.
I hope someone knows the solution to this problem. Thank you for your time and effort!
<?xml version="1.0"?>
<xsl:stylesheet version="1.0">
<xsl:output media-type="xml" encoding="ISO-8859-1" indent="yes"/>
<xsl:key name="object-map" match="/Data/Objects/*" use="@ExternalId"/>
<xsl:template match="/">
<Data>
<Objects>
...
...
...
<xsl:for-each select="Data/Objects/Cell">
<xsl:attribute name="PpXmlVer">
<xsl:text>7.0</xsl:text>
</xsl:attribute>
......................
<!--Calculating Machine Time: -->
<Cell>
<xsl:attribute name="ExternalId">
<xsl:value-of select="@ExternalId"/>
</xsl:attribute>
<!-- calculated.-->
<FlipMachineTime>
<xsl:choose>
<xsl:when test="./FlipPlanedOccupationTime > 0">
<xsl:value-of select="here is a complicated formula to compute"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</FlipMaschineTime>
</Cell>
</xsl:for-each>
Here I would like to have the sum of FlipMachineTime
for all encountered elements of type cell.
...........
</Objects>
</Data>
You can format your XSLT stylesheet to go to a specific node, and then loop through the given node set. You create an XSLT loop with the <xsl:for-each> tag. The value of the select attribute in this tag is an XPath expression that allows you to specify the data element to loop through.
yes, they are mutually exclusive - refer to stackoverflow.com/questions/18742988/…
You need to create a variable to hold the FlipMachineTime nodes you have computed. Then you can sum the nodeset. Here is some sample code:
<xsl:variable name="flipMachineTimes">
<xsl:for-each select="/Data/Objects/Cell">
<FlipMachineTime>
<xsl:choose>
<xsl:when test="./FlipPlanedOccupationTime > 0">
<xsl:value-of select="here is a complicated formula to compute"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</FlipMachineTime>
</xsl:for-each>
</xsl:variable>
<total>
<xsl:variable name="myTotal" select="xalan:nodeset($flipMachineTimes)"/>
<xsl:value-of select="sum($myTotal/FlipMachineTime)"/>
</total>
To get this to work, make sure you include the xalan namespace in your stylesheet:
<xsl:stylesheet version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With