2

我试图在 XSL v1.0 中分别总结正值和负值。

我有这样的 XML:

<Billing_Statements>
    <Statement_Details>
        <Invoice_Details>
            <Meter_Details>
                <Consumption>XX</Consumption>
            </Meter_Details>
            <Meter_Details>
                <Consumption>XX</Consumption>
            </Meter_Details>
        </Invoice_Details>
        <Invoice_Details>
            <Meter_Details>
                <Consumption>XX</Consumption>
            </Meter_Details>
            <Meter_Details>
                <Consumption>XX</Consumption>
            </Meter_Details>
        </Invoice_Details>
    </Statement_Details>
</Billing_Statements>

其中 XX 可以是正值或负值。

我需要首先输出每个 Statement_Details 的所有 Invoice_Detail 节点的正值总和,然后输出负值总和。

目前我已经尝试过:

<xsl:value-of select="sum(Invoice_Details/Meter_Details[Consumption &lt; 0])" />

以及这个的许多变体,无论如何它们都会返回一个总和0

从 Meter_Details 节点收集所有其他信息时,我的 for-each 部分可以完美地工作,如下所示:

<xsl:for-each select="Invoice_Details/Meter_Details[Consumption &lt; 0]">

并且只选择具有负值或正值的节点。为什么这对 sum() 不起作用?

我当然在这里与 XSL 作斗争,并希望能得到任何帮助。

4

1 回答 1

0

使用

sum(*/*/*[. >=0])

sum(*/*/*[not(. >=0)])

这是一个使用这两个相对 XPath 表达式的完整转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="Statement_Details">

Statement <xsl:value-of select="position()"/>
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="Invoice_Details">
  Invoice <xsl:value-of select="position()"/>
    Positive: <xsl:value-of select="sum(*/*[. >=0])"/>
    Negative: <xsl:value-of select="sum(*/*[not(. >=0)])"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档(具有所需结构)时:

<Billing_Statements>
    <Statement_Details>
        <Invoice_Details>
            <Meter_Details>
                <Consumption>-3</Consumption>
            </Meter_Details>
            <Meter_Details>
                <Consumption>5</Consumption>
            </Meter_Details>
        </Invoice_Details>
        <Invoice_Details>
            <Meter_Details>
                <Consumption>8</Consumption>
            </Meter_Details>
            <Meter_Details>
                <Consumption>-12</Consumption>
            </Meter_Details>
        </Invoice_Details>
        <Invoice_Details>
            <Meter_Details>
                <Consumption>22</Consumption>
            </Meter_Details>
            <Meter_Details>
                <Consumption>3</Consumption>
            </Meter_Details>
        </Invoice_Details>
        <Invoice_Details>
            <Meter_Details>
                <Consumption>4</Consumption>
            </Meter_Details>
            <Meter_Details>
                <Consumption>-5</Consumption>
            </Meter_Details>
        </Invoice_Details>
    </Statement_Details>
    <Statement_Details>
        <Invoice_Details>
            <Meter_Details>
                <Consumption>-13</Consumption>
            </Meter_Details>
            <Meter_Details>
                <Consumption>25</Consumption>
            </Meter_Details>
        </Invoice_Details>
        <Invoice_Details>
            <Meter_Details>
                <Consumption>77</Consumption>
            </Meter_Details>
            <Meter_Details>
                <Consumption>-15</Consumption>
            </Meter_Details>
        </Invoice_Details>
        <Invoice_Details>
            <Meter_Details>
                <Consumption>31</Consumption>
            </Meter_Details>
            <Meter_Details>
                <Consumption>-3</Consumption>
            </Meter_Details>
        </Invoice_Details>
        <Invoice_Details>
            <Meter_Details>
                <Consumption>-87</Consumption>
            </Meter_Details>
            <Meter_Details>
                <Consumption>54</Consumption>
            </Meter_Details>
        </Invoice_Details>
    </Statement_Details>
</Billing_Statements>

产生了想要的正确结果:

Statement 1
  Invoice 1
    Positive: 5
    Negative: -3
  Invoice 2
    Positive: 8
    Negative: -12
  Invoice 3
    Positive: 25
    Negative: 0
  Invoice 4
    Positive: 4
    Negative: -5

Statement 2
  Invoice 1
    Positive: 25
    Negative: -13
  Invoice 2
    Positive: 77
    Negative: -15
  Invoice 3
    Positive: 31
    Negative: -3
  Invoice 4
    Positive: 54
    Negative: -87
于 2013-03-19T04:00:56.760 回答