【问题标题】:XSL v1.0 and XPath to Traverse Deep Nesting and Recursively Sum all LinesXSL v1.0 和 XPath 遍历深度嵌套并递归求和所有行
【发布时间】: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>&#10;</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

也就是说,将所有&lt;LI_Amount_display/&gt;节点相加,这样就可以输出到一个固定宽度的平面文件中。

具体问题在于“total”模板中for-each 循环中的XPath。我还没有看到我在那个循环中,因此,select 在某种程度上是错误的......但是,嗯,这就是我在这里的原因。

最后,它必须是 XSL 1.0 版。

如果您需要进一步说明,请告诉我。

【问题讨论】:

  • 为什么最终结果应该是11000.00 而不是9000.00?据我所知,只有 3 个 LI_Amount_display 元素。
  • 是的,显然我在学校学会了编码,但没有添加...... 9000.00 应该是......已编辑

标签: xslt xpath recursion xslt-1.0


【解决方案1】:

我不确定我是否理解您的要求。当最终结果应该只是一个总数时,为什么要编写如此复杂的样式表呢?此外,您似乎已经熟悉相关的 EXSLT 函数以及将字符串转换为数字。

样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:exsl="http://exslt.org/common">
    <xsl:output method="text" encoding="UTF-8" indent="no"/>

    <xsl:variable name="amounts">
        <xsl:for-each select="//LI_Amount_display">
            <amount>
                <xsl:value-of select="number(substring(translate(.,',',''),2))"/>
            </amount>
        </xsl:for-each>
    </xsl:variable>

    <xsl:template match="/">
        <xsl:value-of select="sum(exsl:node-set($amounts)/amount)"/>
    </xsl:template>

</xsl:stylesheet>

输出

9000

【讨论】:

  • 复杂性是由于我取出的其他代码的切线性质......不幸的是,试图将方形钉安装在圆孔中......接收系统周围的不灵活约束只有一种方式的数据......而且(更重要的是)XSL 是我上个月学习的东西,所以我边走边学
  • @RebelPhoenix 是的,我猜你正在尝试做一些复杂的事情。但是说你的代码库很复杂并且你面临着不灵活的约束绝对不能帮助 me 帮助你。所以,再说一遍:为什么你不能使用像上面这样简单的方法?
  • 由于我编写了其余代码的方式,我必须从fixTheWidth 模板中输出值。我在我的 IDE 中运行了它,它工作正常。但是当我部署到目标系统时,我得到了一个异常:javax.xml.transform.TransformerException: org.xml.sax.SAXException: xsl:template is not allowed in this position in the stylesheet! 我尝试了一些方法让它工作但无济于事。
【解决方案2】:

虽然我倾向于接受 Mathias Müller 的建议,但我想展示如何使用递归命名模板来做到这一点:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <xsl:call-template name="sum-nodes" >
        <xsl:with-param name="nodes" select="query/results/result/columns/column/LI_Amount_display" />
    </xsl:call-template>
</xsl:template>

<xsl:template name="sum-nodes" >
    <xsl:param name="nodes"/>
    <xsl:param name="sum" select="0"/>
    <xsl:param name="newSum" select="$sum + translate($nodes[1], '$,', '')"/>
    <xsl:choose>
        <xsl:when test="count($nodes) > 1">
             <!-- recursive call --> 
            <xsl:call-template name="sum-nodes" >
                <xsl:with-param name="nodes" select="$nodes[position() > 1]" />
                <xsl:with-param name="sum" select="$newSum" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="format-number($newSum, '#,##0.00')"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

结果

9,000.00

【讨论】:

  • 是的,我愿意倾向于 Mathias 解决方案的简单性。但我无法让它在目标系统中工作。您的解决方案成功了。
  • 如果 XSL 的新手遇到此问题,请完成我在 &lt;xsl:otherwise&gt;... 语句的 select 中添加的代码 format-number($newSum, '0.##'),以便将值输出到小数点后两位
  • 将数字格式化为'0.##' 会将输出值限制为最多 两位小数。换句话说,它只影响小数点后三位或更多位的值。在上面的例子中它不会有任何影响。使用'0.00'所有值标准化为精确两位小数。
猜你喜欢
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-08
相关资源
最近更新 更多