XSL v1.0和XPath遍历深度嵌套并递归求和所有行

Reb*_*nix 0 xslt recursion xpath xslt-1.0

我到处都在寻找其他解决方案,并尝试了各种方法,但仍然无济于事...

这是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>
Run Code Online (Sandbox Code Playgroud)

到目前为止,这里是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>
Run Code Online (Sandbox Code Playgroud)

最终结果

9000.00

换句话说,对所有<LI_Amount_display/>节点求和,以便可以将其输出到固定宽度的平面文件中。

具体问题围绕for-each“总计”模板中循环中的XPath 。我还没有看到我处于那个循环中,因此,select在某种程度上是错误的……但是,好吧,这就是为什么我在这里。

最后,它必须与XSL 1.0版一起使用。

让我知道您是否需要进一步说明。

Mat*_*ler 5

我不确定我是否理解您的要求。当最终结果应该只是一个总数时,为什么还要编写如此复杂的样式表?同样,您似乎已经熟悉相关的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>
Run Code Online (Sandbox Code Playgroud)

输出量

9000
Run Code Online (Sandbox Code Playgroud)

  • @RebelPhoenix是的,我搜集到您正在尝试做一些复杂的事情。但是,说您的代码库很复杂,并且遇到了不灵活的约束,绝对不会帮助_me_帮助您。所以,再说一遍:为什么不能使用像上面的方法那样简单的方法? (2认同)