XSLT:对来自另一个字段的借方或贷方确定的金额字段的差异/总和函数计算

时间:2018-01-22 05:07:29

标签: xslt

  

搜索但找不到相关帖子 - 希望有人可以提供帮助。

     

我正在试图弄清楚如何获得所有信用额之和与给定公司的所有借方之和的差异 - 这在期刊中只生成一行。

     

第三个字段确定下面示例中的金额是借方还是贷方。

     

该文件包含许多字段(管道分隔),但此处只需要以下内容:

* Col1 = Companies
* Col2 = Amount (All positive values)
* Col3 = Amount Type containing Credits or Debits ("C" or "D" values)


File Example:
----------------
A|200.00|D
A|250.00|C
A|100.00|D
B|50.00|D
B|25.00|D
C|20.00|D
C|25.00|C
C|10.00|D
C|5.00|D

The rows for these sums should be: sum(Debits) - sum(Credits)
Company A should = 50.00 (300.00 - 250.00)
Company B should = 75.00 (75.00 - 0.00)
Company C should = 10.00 (35.00 - 25.00)
  

如何修复计算? - 代码底部。

     

我的代码总结了总金额而没有区分信用和借记,但我需要这两笔金额之间的差异。

     

对于公司“A”,以下代码产生550.00而不是所需的50.00总和(借方) - 总和(积分)

<xsl:value-of select="sum(currentgroup()/col2)"/>

Code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="root">
    <!-- TODO: Auto-generated template -->
    <root>

    <!-- All companies except "A" Company -->
    <xsl:for-each-group  select="row[col1 !='A']" group-by ="col1">
        <journal>
            <xsl:for-each select="current-group()">
                <row>
                    <Company>
                        <xsl:value-of select="col1"/>
                    </Company>
                    <Amount>
                        <xsl:value-of select="col2"/>
                    </Amount>
                    <AmountType>
                        <xsl:value-of select="col3"/>
                    </AmountType>
                </row>
            </xsl:for-each>
        </journal>                      
    </xsl:for-each-group>

    <!-- only for "A" -->
        <journal>
            <xsl:for-each-group  select="row" group-by ="col1">
            <xsl:choose>

            <!-- The details from input file for Company "A" -->        
            <xsl:when test="current-group()/col1='A'">
                    <xsl:for-each select="current-group()">
                    <row>
                    <Company>
                        <xsl:value-of select="col1"/>
                    </Company>
                    <Amount>
                        <xsl:value-of select="col2"/>
                    </Amount>
                    <AmountType>
                        <xsl:value-of select="col3"/>
                    </AmountType>
                </row>
                </xsl:for-each>
            </xsl:when>                                     
            <xsl:otherwise>
            <xsl:for-each  select="current-group()[1]">
                <row>
                    <Company>
                        <xsl:value-of select="'A'"/>
                    </Company>
                    <Amount>   
                       <xsl:value-of select="sum(current-group()/col2)"/> 
                    </Amount>
                    <AmountType>
                        <xsl:value-of select="col3"/>
                    </AmountType>   
                </row>
                </xsl:for-each>
                </xsl:otherwise>                    
            </xsl:choose>
            </xsl:for-each-group>
        </journal>  
    </root> 
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

您目前正在执行此操作以获取总和...(或者更确切地说,您正在使用XSLT中的连字符namespace WebsocketServer; //load the cakePHP classes require_once '../config/bootstrap.php'; use Cake\ORM\TableRegistry; class Server { public function query() { //any data fetch mockup (the $data = TableRegistry::get('Screens')->find()->all()->toArray(); } } $server = new Server(); //querying works perfecly $server->query(); //wait for a day sleep(60*60*24); //querying results in an error $server->query(); ,这是一个错字)。

currentgroup()

假设 <xsl:value-of select="sum(current-group()/col2)"/> 拥有col3C,那么你可以这样做

D