开发者

xslt V1.0 - subtemplate with recursive loop returns empty value

I'm trying to get the highest value of the sum of the childs of each cluster.

  • cluster1 : 10 + 20 = 30

  • cluster2 : 20 + 30 = 50 --> 50 is highest value

Problem: The return value of the subtemplate is "".

why? The variable tempMax is getting a node with my number in it instead of 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>

return of $max

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


You are missing the opposite case in 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've tested it (changed using xsl:choose as suggested by @Mads, even if is 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 on the input provided in the question, returns 50.

Applied on this changed 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜