我遇到了XSLT问题:我喜欢用参数计算节点总和。
XML源代码如下:
<Documents>
<Document>
<Deleted>0</Deleted>
<DocumentType>2</DocumentType>
<Currency>EUR</Currency>
<CurrencyRate>4.368400</CurrencyRate>
<GrossValue>1000.00</GrossValue>
<DeliveryDate>2016-08-01</DeliveryDate>
<FormOfPayment>2</FormOfPayment>
<DueDate>2016-09-28</DueDate>
</Document>
<Document>
<Deleted>0</Deleted>
<DocumentType>2</DocumentType>
<Currency>EUR</Currency>
<CurrencyRate>4.368400</CurrencyRate>
<GrossValue>2000.00</GrossValue>
<DeliveryDate>2016-08-05</DeliveryDate>
<FormOfPayment>5</FormOfPayment>
<DueDate>2016-09-05</DueDate>
</Document>
<Document>
<Deleted>0</Deleted>
<DocumentType>2</DocumentType>
<Currency>EUR</Currency>
<CurrencyRate>4.368400</CurrencyRate>
<GrossValue>3000.00</GrossValue>
<DeliveryDate>2016-08-30</DeliveryDate>
<FormOfPayment>2</FormOfPayment>
<DueDate>2016-10-29</DueDate>
</Document>
<Document>
<Deleted>0</Deleted>
<DocumentType>2</DocumentType>
<Currency>EUR</Currency>
<CurrencyRate>4.368400</CurrencyRate>
<GrossValue>2500.00</GrossValue>
<DeliveryDate>2016-08-26</DeliveryDate>
<FormOfPayment>5</FormOfPayment>
<DueDate>2016-09-10</DueDate>
</Document>
</Documents>
我需要的结果是参数FormOfPayment
的总量GrossValue。
FormOfPayment
为5,则变量GrossValueCard
应为4500 FormOfPayment
为2时,变量GrossValueCash
应为4000. 我试着这样做:
<xsl:template name="Suma">
<xsl:param name="index" select="1"/>
<xsl:param name="nodes"/>
<xsl:param name="totalAmount" select="0"/>
<xsl:variable name="currentAmount" select="translate($nodes[$index],',','.')"/>
<xsl:choose>
<xsl:when test="count($nodes)=0">
<xsl:value-of select="0"/>
</xsl:when>
<xsl:when test="$index=count($nodes)">
<xsl:value-of select="$totalAmount + $currentAmount"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="Suma">
<xsl:with-param name="index" select="$index+1"/>
<xsl:with-param name="totalAmount" select="$totalAmount + $currentAmount"/>
<xsl:with-param name="nodes" select="$nodes"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
并且它可以工作,但总和来自所有GrossValue
个节点。你能给我一些建议,我应该设置一个参数来选择FormOfPayment
之和吗?
我试图选择条件&#34;当&#34;或&#34;如果&#34;,但它没有工作。
答案 0 :(得分:2)
我需要的结果是参数的总量GrossValue
FormOfPayment
。
那你为什么不这样做呢?
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="FormOfPayment"/>
<xsl:template match="/Documents">
<total>
<xsl:value-of select="sum(Document[FormOfPayment=$FormOfPayment]/GrossValue)"/>
</total>
</xsl:template>
</xsl:stylesheet>
<强> ADDED 强>
如果 - 从您的尝试中看起来似乎 - XML源中的金额使用十进制逗号而不是十进制点(尽管显示的XML 不表现出此问题),并且您仅限于XSLT 1.0,我建议你这样做:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="FormOfPayment"/>
<xsl:template match="/Documents">
<xsl:variable name="amounts">
<xsl:for-each select="Document[FormOfPayment=$FormOfPayment]">
<amount>
<xsl:value-of select="translate(GrossValue, ',', '.')"/>
</amount>
</xsl:for-each>
</xsl:variable>
<total>
<xsl:value-of select="sum(exsl:node-set($amounts)/amount)"/>
</total>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
您可以使用GrossValue
之类的谓词来选择特定的GrossValue[../FormOfPayment = 'x']
个节点
要添加这些选定值,请使用带有此谓词表达式的XSLT 1.0函数sum(...)
。
样式表可能如下所示:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" />
<xsl:template match="text()" />
<xsl:template match="/Documents">
<xsl:variable name="GrossValueCard2">
<xsl:value-of select="sum(Document/GrossValue[../FormOfPayment = '2']/text())" />
</xsl:variable>
<xsl:variable name="GrossValueCard5">
<xsl:value-of select="sum(Document/GrossValue[../FormOfPayment = '5']/text())" />
</xsl:variable>
Sum of 'FormOfPayment 2's: <xsl:value-of select="$GrossValueCard2" />
Sum of 'FormOfPayment 5's: <xsl:value-of select="$GrossValueCard5" />
</xsl:template>
</xsl:stylesheet>