我试图获得每个群集的孩子总和的最高值.
> cluster1: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 > 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 < $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 < $barSum"> <xsl:value-of select="$barSum"/> </xsl:if> <xsl:if test="$max >= $barSum"> <xsl:value-of select="$max"/> </xsl:if> </xsl:variable>
这就是我测试它的方式(使用xsl更改:按照@Mads的建议选择,即使在逻辑上等效).
[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 > 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 < $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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。