c# – xslt V1.0 – 带有递归循环的子模板返回空值
发布时间:2020-12-15 17:18:47 所属栏目:百科 来源:网络整理
导读:我试图获得每个群集的孩子总和的最高值. cluster1:10 20 = 30 cluster2:20 30 = 50 – 50是最高值 问题:子模板的返回值是“”. 为什么?变量tempMax正在获取一个包含我的数字的节点,而不仅仅是一个数字. $tempMax = {Dimension:[1]}+ [1] = /+ + node()[1
我试图获得每个群集的孩子总和的最高值.
> cluster1:10 20 = 30 问题:子模板的返回值是“”. $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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |