Xml XSL如何使用“和”到foreach来执行赔率程序?
我有个问题。如何对Xml XSL如何使用“和”到foreach来执行赔率程序?,xml,xslt,Xml,Xslt,我有个问题。如何对的结果进行求和?结果只能显示一次。这就是我所看到的:“0.1,0.3”,但我想要这些值的总和 我试着把所有的东西都放在一个变量里,但我没有得到任何东西,我想它不允许我把变量放在其他变量里。 那么,我能做什么 XML XSLT 由于XSLT1.0不允许在sum()函数中使用XPath,因此使用XSLT1.0对计算值求和有点麻烦。如果您可以自由升级到XSLT 2.0processor,那么使用sum()函数对计算进行求和会容易得多 但是,如果您没有此选项,并且必须使用XSLT
的结果进行求和?结果只能显示一次。这就是我所看到的:“0.1,0.3”,但我想要这些值的总和
我试着把所有的东西都放在一个变量里,但我没有得到任何东西,我想它不允许我把变量放在其他变量里。
那么,我能做什么
XML
XSLT
由于XSLT1.0不允许在
sum()
函数中使用XPath,因此使用XSLT1.0
对计算值求和有点麻烦。如果您可以自由升级到XSLT 2.0
processor,那么使用sum()
函数对计算进行求和会容易得多
但是,如果您没有此选项,并且必须使用XSLT 1.0
,那么您必须使用递归模板调用来执行计算,然后继续添加所有计算以生成最终和
下面是有帮助的XSLT
<xsl:stylesheet version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xalan">
<xsl:output method="xml" />
<xsl:strip-space elements="*" />
<xsl:template match="arbol">
<xsl:variable name="n" select="@n" />
<xsl:variable name="f" select="atributo/valor/@f" />
<xsl:for-each select="atributo/valor/atributo">
<summation>
<xsl:call-template name="ProductSum">
<xsl:with-param name="nodeSet" select="*" />
<xsl:with-param name="n" select="$n" />
<xsl:with-param name="f" select="$f" />
</xsl:call-template>
</summation>
</xsl:for-each>
</xsl:template>
<xsl:template name="ProductSum">
<xsl:param name="nodeSet" />
<xsl:param name="n" />
<xsl:param name="f" />
<xsl:param name="sum" select="0" />
<xsl:variable name="nodeStart" select="$nodeSet[1]" />
<xsl:variable name="nodeEnd" select="$nodeSet[position() > 1]" />
<!-- Perform computation for the current node -->
<xsl:variable name="currSum">
<xsl:choose>
<xsl:when test="$nodeStart/@encendido = '1'">
<xsl:value-of select="format-number(($nodeStart/@f div $f) * ($f div $n), '#0.0')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="0" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Check whether the current node is not the last node in the node-set -->
<xsl:choose>
<xsl:when test="not($nodeEnd)">
<xsl:value-of select="$sum + $currSum" />
</xsl:when>
<xsl:otherwise>
<!-- Recursive call to the same template -->
<xsl:call-template name="ProductSum">
<xsl:with-param name="nodeSet" select="$nodeEnd" />
<xsl:with-param name="n" select="$n" />
<xsl:with-param name="f" select="$f" />
<xsl:with-param name="sum" select="$sum + $currSum" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
输出
<summation>0.4</summation>
0.4
下面是一个类似的问题,该解决方案显示了使用XSLT 2.0
和XSLT 1.0扩展函数生成求和的附加选项
看看您的计算,xsl:value of中的表达式的形式是(A div B)*(B div C)
,它简化为(A div C)
。由于C对于每个valor
都是相同的,所以这里不需要对每个值使用xsl:for,也不需要尝试对计算值求和。您只需执行
。再见!我没想到!再次谢谢谢谢你!你是最棒的,我没想到这会如此混乱哈哈,我想这会比你给我看的更容易,不管怎样,谢谢!(我不知道如何使用这个平台,所以我不知道我是否在回复你的评论,我希望是,如果不是,我很抱歉:)
<xsl:stylesheet version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xalan">
<xsl:output method="xml" />
<xsl:strip-space elements="*" />
<xsl:template match="arbol">
<xsl:variable name="n" select="@n" />
<xsl:variable name="f" select="atributo/valor/@f" />
<xsl:for-each select="atributo/valor/atributo">
<summation>
<xsl:call-template name="ProductSum">
<xsl:with-param name="nodeSet" select="*" />
<xsl:with-param name="n" select="$n" />
<xsl:with-param name="f" select="$f" />
</xsl:call-template>
</summation>
</xsl:for-each>
</xsl:template>
<xsl:template name="ProductSum">
<xsl:param name="nodeSet" />
<xsl:param name="n" />
<xsl:param name="f" />
<xsl:param name="sum" select="0" />
<xsl:variable name="nodeStart" select="$nodeSet[1]" />
<xsl:variable name="nodeEnd" select="$nodeSet[position() > 1]" />
<!-- Perform computation for the current node -->
<xsl:variable name="currSum">
<xsl:choose>
<xsl:when test="$nodeStart/@encendido = '1'">
<xsl:value-of select="format-number(($nodeStart/@f div $f) * ($f div $n), '#0.0')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="0" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Check whether the current node is not the last node in the node-set -->
<xsl:choose>
<xsl:when test="not($nodeEnd)">
<xsl:value-of select="$sum + $currSum" />
</xsl:when>
<xsl:otherwise>
<!-- Recursive call to the same template -->
<xsl:call-template name="ProductSum">
<xsl:with-param name="nodeSet" select="$nodeEnd" />
<xsl:with-param name="n" select="$n" />
<xsl:with-param name="f" select="$f" />
<xsl:with-param name="sum" select="$sum + $currSum" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<summation>0.4</summation>