c# - xslt V1.0 - 具有递归循环的子模板返回空值

标签 c# .net xml xslt xslt-1.0

我正在尝试获取每个集群的 child 总和的最大值。

  • 集群 1:10 + 20 = 30

  • cluster2 : 20 + 30 = 50 --> 50 是最高值

问题:子模板的返回值为“”。
为什么?变量 tempMax 正在获取一个节点,其中包含我的数字,而不仅仅是一个数字。

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

我该如何解决这个问题? (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>

我的 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>

返回$max

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

最佳答案

您在分配 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>

这就是我测试它的方式(根据@Mads 的建议使用 xsl:choose 进行了更改,即使在逻辑上是等效的)。

[XSLT 1.0] 使用 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>

应用于问题中提供的输入,返回 50

应用于这个改变的输入:

<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>

返回 100

关于c# - xslt V1.0 - 具有递归循环的子模板返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6864601/

相关文章:

.NET获取所有用户均可访问的本地临时目录

css - 从 MVC 模型以编程方式设置 css 值

XML 到 CSV 应用 XSLT(格式问题、逻辑错误)

xml - 在 XML 中添加新的换行/换行标记

c# - MySqlDataReader.GetBytes

c# - 使游戏对象不可见而不使其处于非事件状态

c# - 用于注册 ActiveX 的 .INF 文件问题

c# - 具有特定值的动画 ProgressBar 内容 WPF

.net - 您创建 stub 的首选工具是什么?

php - 寻找有关计算机科学类(class)项目的想法