我试图在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 < 0])" />
以及它的许多变体,无论如何都会返回0
的总和。
当从Meter_Details节点收集所有其他信息时,我的for-each部分完美地工作:
<xsl:for-each select="Invoice_Details/Meter_Details[Consumption < 0]">
并仅选择具有负值或正值的节点。为什么这对sum()不起作用?
我肯定在这里与XSL斗争,并且会喜欢我能得到的任何帮助。
答案 0 :(得分: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