我试图在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挣扎,希望得到我能提供的任何帮助。

最佳答案

用:

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

关于xml - 分别对负值和正值求和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15491152/

10-11 21:19