使用XSLT从XML动态数据操作(Dynamic Data Manipulation using X

2019-10-22 19:48发布

我不与XSLT工作的时间不会太长。 我读了XSLT的变量不能在飞行中更新,所以我如何能做到下面的任务。

我想总结一下购买与销售,并把它们放入一个变量,并根据这些数值做出一些决定。 (例如,如果购买是大于销售那么,如果没有做的事,做别的事情)

<rows>
    <row>
        <col attr2="Purchase" >100.00</col>
        <col  attr2="Sales" >100.00</col>
    </row>
    <row >
        <col attr2="Purchase" >19.16</col>
        <col  attr2="Sales" >12.94</col>
    </row>
    <row >
        <col attr2="Purchase" >0.67</col>
        <col  attr2="Sales" >2.74</col>
    </row>
    <row >
        <col attr2="Purchase" >71.95</col>
        <col  attr2="Sales" >61.54</col>
    </row>
    <row >
        <col attr2="Purchase" >3.62</col>
        <col  attr2="Sales" >14.72</col>
    </row>
    <row >
        <col attr2="Purchase">8.80</col>
        <col attr2="Sales">1.22</col>
    </row>
    <row >
        <col attr2="Purchase" >-4.28</col>
        <col  attr2="Sales" >6.53</col>
    </row>
</rows>

如果有人知道,请帮助我。

Answer 1:

XSL变量是多个常量:一旦设定,其价值不能被改变。 改变一个变量的唯一方法是使用递归模板,并使用名为参数保持当前总和。

或者你会,如果XSLT没有一个sum的功能!

<xsl:variable name="$purchase-total" select="sum(col[@attr2='Purchase'])" />
<xsl:variable name="$sales-total" select="sum(col[@attr2='Sales'])" />
<xsl:choose>
    <xsl:when test="$purchase-total &gt; $sales-total">
        <!-- Do something -->
    </xsl:when>
    <xsl:otherwise>
        <!-- Do something -->
    </xsl:otherwise>
</xsl:choose>


Answer 2:

你可以计算的金额如图@Eric的例子。

这个问题在您的评论问:要计算的绝对值x使用以下XPath表达式:

(x > 0)*x - not(x > 0)*x

例如

随着所提供的XML文档,

  <xsl:variable name="x" select="(/*/*/col[@attr2='Purchase'])[position()=last()]"/>

  <xsl:value-of select="($x > 0)*$x - not($x > 0)*$x"/>

生产

4.28


文章来源: Dynamic Data Manipulation using XSLT from XML
标签: xml xslt