【发布时间】:2014-12-19 19:28:03
【问题描述】:
我一直在寻找其他解决方案,并尝试了各种方法,但仍然没有 avial...
这是 XML 结构...
<?xml version="1.0" encoding="UTF-8"?>
<query>
<continueToken/>
<results total="15">
<result recordId="16672888">
<columns>
<column>
<field>AmountNU</field>
<LI_Amount><![CDATA[$4,000.00]]></LI_Amount>
<LI_Amount_display><![CDATA[$4,000.00]]></LI_Amount_display>
</column>
</columns>
</result>
<result recordId="16672889">
<columns>
<column>
<field>AmountNU</field>
<LI_Amount><![CDATA[$3,000.00]]></LI_Amount>
<LI_Amount_display><![CDATA[$3,000.00]]></LI_Amount_display>
</column>
</columns>
</result>
<result recordId="16672890">
<columns>
<column>
<field>AmountNU</field>
<LI_Amount><![CDATA[$2,000.00]]></LI_Amount>
<LI_Amount_display><![CDATA[$2,000.00]]></LI_Amount_display>
</column>
</columns>
</result>
</results>
</query>
这是目前为止的 XSL。这只是较大脚本的一小部分,因此您将看到与当前问题无关的其他内容。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:datetime="http://exslt.org/dates-and-times"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="datetime">
<xsl:output method="text" encoding="UTF-8" indent="no"/>
<xsl:template match="/">
<xsl:call-template name="fixTheWidth" >
<!-- This parameter is a Id for each group of records based on the result/@recordId attribute. This groups all records to the record ID-->
<xsl:with-param name="resultIndex" select="//results/result[generate-id(.) = generate-id(key('recordID', @recordId)[1])]" />
</xsl:call-template>
</xsl:template>
<!--************************************************************************
TEMPLATE: fixTheWidth
PURPOSE: Where all the magic happens
*************************************************************************-->
<xsl:template name="fixTheWidth" match="/results">
<xsl:param name="resultIndex" /> <!-- A unique index based on grouping the records on the recordID -->
<!-- more code here, uses $resultIndex -->
<!-- Field Name: TOTAL INVOICE AMOUNT, Length = 9 (49-57), Format: numeric -->
<xsl:variable name="amount">
<xsl:call-template name="total">
<xsl:with-param name="sum" select="number(0)" />
<xsl:with-param name="startLine" select="//LI_Amount_display" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$amount" />
</xsl:template><!-- END of 'fixTheWidth' template -->
<!--****************************************************************************
TEMPLATE: total
PURPOSE: Add the total amount of all line items being processed.
*****************************************************************************-->
<xsl:template name="total" >
<xsl:param name="startLine"/>
<xsl:param name="sum"/>
<xsl:param name="newSum" select="$sum + number(translate(substring-after($startLine, '$'), ',', ''))"></xsl:param>
<!-- TEST STUB -->
<xsl:variable name="amount" select="number(translate(substring-after($startLine, '$'), ',', ''))" />
<xsl:value-of select="number(translate(substring-after($startLine, '$'), ',', ''))" />
<xsl:text> | </xsl:text>
<xsl:value-of select="$newSum + $amount" />
<xsl:text> </xsl:text>
<xsl:for-each select="$startLine/ancestor::results/following-sibling::result/columns/column/LI_Amount_display">
<!-- STUB -->
<xsl:text> for each </xsl:text>
<xsl:value-of select="number(translate(substring-after(., '$'), ',', ''))" />
<!-- Recursively Sum -->
<xsl:choose>
<xsl:when test="position() = last()">
<xsl:value-of select="$newSum"/>
</xsl:when>
<xsl:when test="$startLine/ancestor::results/following-sibling::result/columns/column/LI_Amount_display">
<xsl:call-template name="total" >
<xsl:with-param name="startLine" select="." />
<xsl:with-param name="sum" select="$newSum" />
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
最终结果
9000.00
也就是说,将所有<LI_Amount_display/>节点相加,这样就可以输出到一个固定宽度的平面文件中。
具体问题在于“total”模板中for-each 循环中的XPath。我还没有看到我在那个循环中,因此,select 在某种程度上是错误的......但是,嗯,这就是我在这里的原因。
最后,它必须是 XSL 1.0 版。
如果您需要进一步说明,请告诉我。
【问题讨论】:
-
为什么最终结果应该是
11000.00而不是9000.00?据我所知,只有 3 个LI_Amount_display元素。 -
是的,显然我在学校学会了编码,但没有添加...... 9000.00 应该是......已编辑
标签: xslt xpath recursion xslt-1.0