Xslt V1.0 - a sub-topic with a recursive loop returns an empty value

I am trying to get the maximum value of the sum of the children of each cluster.

  • cluster1: 10 + 20 = 30

  • cluster2: 20 + 30 = 50 → 50 - the highest value

Problem . The return value of the substring is "".
What for? The tempMax variable gets a node with my number, not just a number.

$tempMax = {Dimension:[1]} + [1] = / + + node()[1] = 50 

How can i fix this? (xslt v1.0).


XML:

 <?xml version="1.0"?> <column-chart-stacked-full> <clusters> <cluster number="1"> <bar> <value>10</value> </bar> <bar> <value>20</value> </bar> </cluster> <cluster number="2"> <bar> <value>20</value> </bar> <bar> <value>30</value> </bar> </cluster> </clusters> </column-chart-stacked-full> 

my xsl:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <xsl:variable name="highestClusterVal"> <xsl:call-template name="findMaxClusterVal"/> </xsl:variable> <xsl:template name="findMaxClusterVal"> <xsl:param name="count" select="count(column-chart-stacked- full/clusters/cluster)"/> <xsl:param name="limit" select="$count"/> <xsl:param name="max" select="0"/> <xsl:choose> <xsl:when test="$count &gt; 0"> <xsl:variable name ="barSum" select="sum(column-chart-stacked-full/clusters/cluster[$count]/bar/value)"/> <xsl:variable name="tempMax"> <xsl:choose> <xsl:when test="$max &lt; $barSum"> <xsl:value-of select="$barSum"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$max"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <!-- recursive loop --> <xsl:call-template name="findMaxClusterVal"> <xsl:with-param name="count" select="$count - 1"/> <xsl:with-param name="limit" select="$limit"/> <xsl:with-param name="max" select="$tempMax"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <!-- return max value --> <xsl:value-of select="$max"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> 

refund $ max

 $max = {Dimension:[1]} + [1] = / + + node()[1] = 50 
+4
source share
1 answer

You are missing the opposite case when assigning tempMax :

  <xsl:variable name="tempMax"> <xsl:if test="$max &lt; $barSum"> <xsl:value-of select="$barSum"/> </xsl:if> <xsl:if test="$max >= $barSum"> <xsl:value-of select="$max"/> </xsl:if> </xsl:variable> 

This is how I tested it (changed using xsl:choose , as suggested by @Mads, even if logically equivalent).

[XSLT 1.0] Tested with Saxon 6.5

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes"/> <xsl:template match="/"> <xsl:call-template name="findMaxClusterVal"/> </xsl:template> <xsl:template name="findMaxClusterVal"> <xsl:param name="count" select="count(column-chart-stacked-full/clusters/cluster)"/> <xsl:param name="limit" select="$count"/> <xsl:param name="max" select="0"/> <xsl:if test="$count &gt; 0"> <xsl:variable name ="barSum" select="sum(column-chart-stacked-full/clusters/cluster[$count]/bar/value)"/> <xsl:variable name="tempMax"> <xsl:choose> <xsl:when test="$max &lt; $barSum"> <xsl:value-of select="$barSum"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$max"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <!-- recursive loop --> <xsl:call-template name="findMaxClusterVal"> <xsl:with-param name="count" select="$count - 1"/> <xsl:with-param name="limit" select="$limit"/> <xsl:with-param name="max" select="$tempMax"/> </xsl:call-template> </xsl:if> <!-- return max value --> <xsl:if test="$count = 0"> <xsl:value-of select="$max"/> </xsl:if> </xsl:template> </xsl:stylesheet> 

applied to the input asked in the question returns 50 .

Applies to this modified input:

 <column-chart-stacked-full> <clusters> <cluster number="1"> <bar> <value>10</value> </bar> <bar> <value>20</value> </bar> </cluster> <cluster number="2"> <bar> <value>20</value> </bar> <bar> <value>30</value> </bar> </cluster> <cluster number="1"> <bar> <value>10</value> </bar> <bar> <value>20</value> </bar> </cluster> <cluster number="2"> <bar> <value>70</value> </bar> <bar> <value>30</value> </bar> </cluster> </clusters> </column-chart-stacked-full> 

Returns 100 .

+2
source

All Articles