代码之家  ›  专栏  ›  技术社区  ›  Imrul

使用XML中的XSLT进行动态数据操作

  •  0
  • Imrul  · 技术社区  · 15 年前

    我没有和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>
    

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

    2 回复  |  直到 15 年前
        1
  •  1
  •   Eric    15 年前

    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>
    
        2
  •  0
  •   Dimitre Novatchev    15 年前

    您可以计算总和,如@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
    
    推荐文章