How can I summarize the results of each cycle in XSL?

I am new to XSL, so I don’t know how to do this. I have a for-each statement that does some calculations for each cell element. How can I summarize the results and store them in a variable so that I can display it? I have included a piece of 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 &gt; 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> 

+6
source share
2 answers

You need to create a variable to store the calculated FlipMachineTime nodes. Then you can summarize the set of nodes. Here is a 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 make this work, make sure you include the xalan namespace in the stylesheet:

 <xsl:stylesheet version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
+3
source

You are better off using exslt:node-set() from http://exslt.org/common than a specific EXSLT processor implementation is much more portable on different processors. In this case, you need to:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" extension-element-prefixes="exslt"> <!-- staff --> </xsl:stylesheet> 

For example, in Saxon 6.5 you can use it as:

 <total> <xsl:variable name="myTotal" select="exslt:node-set($flipMachineTimes)"/> <xsl:value-of select="sum($myTotal/FlipMachineTime)"/> </total> 

If you set the style sheet version to 1.1, it will be simpler (as it should be):

  <total> <xsl:value-of select="sum(exslt:node-set($flipMachineTimes/FlipMachineTime))"/> </total> 

Seeing your meaning, I think you are working on something huge and complex, which cannot be solved here with a simple question. There may be too many details that you might omit. However, if this is just a case of displaying a value (where ???), I can suggest this latter.

Try creating a variable for each cell:

  <Cell> <xsl:attribute name="ExternalId"> <xsl:value-of select="@ExternalId"/> </xsl:attribute> <!-- calculated.--> <FlipMaschinenlaufzeit> <xsl:choose> <xsl:when test="./FlipPlanbelegungszeit &gt; 0"> <xsl:value-of select="$planbelegungszeit - (($planbelegungszeit * ($organisatorischeAusfallzeit div 100)) + ($planbelegungszeit * ($stoerungsbedingteUnterbrechungen div 100)) + ($planbelegungszeit * ($nebenzeit div 100)))"/> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </FlipMaschinenlaufzeit> </Cell> <xsl:varible name="a"> <!-- paste here the code of xsl:choose inside the cell --> </xsl:variable> <!-- create other variables for each cell in the same way (use different names, like b, c ..) --> <!-- after the last cell, display the sum --> <xsl:value-of select="$a + $b + $c + ..."/> 
0
source

Source: https://habr.com/ru/post/889215/


All Articles