我需要计算会计系统过帐的金额,并考虑到他们可能是转让时分配的多个项目,总计不同的份额。
以下是我的XML的简化摘录:
<DATA>
<ACCOUNTING>
<POSTINGS>
<POSTING id="1">
<TYPE>D</TYPE>
<ACCOUNT_ID NAME="Expense Account 555777">555777</ACCOUNT_ID>
<AMOUNT>700</AMOUNT>
<DESCRIPTION>Expense1</DESCRIPTION>
<PROJECT_ID NAME="abc">123</PROJECT_ID>
<SHARE>70</SHARE>
</POSTING>
<POSTING id="2">
<TYPE>D</TYPE>
<ACCOUNT_ID NAME="Expense Account 555777">555777</ACCOUNT_ID>
<AMOUNT>300</AMOUNT>
<DESCRIPTION>Expense1</DESCRIPTION>
<PROJECT_ID NAME="def">456</PROJECT_ID>
<SHARE>30</SHARE>
</POSTING>
<POSTING id="5">
<TYPE>C</TYPE>
<ACCOUNT_ID NAME="Credit Account 111333">111333</ACCOUNT_ID>
<AMOUNT>-1000</AMOUNT>
<DESCRIPTION>Expense1</DESCRIPTION>
</POSTING>
<POSTING id="6">
<TYPE>D</TYPE>
<ACCOUNT_ID NAME="Expense Account 666999">666999</ACCOUNT_ID>
<AMOUNT>7000</AMOUNT>
<DESCRIPTION>Expense2</DESCRIPTION>
<PROJECT_ID NAME="abc">123</PROJECT_ID>
<SHARE>70</SHARE>
</POSTING>
<POSTING id="7">
<TYPE>D</TYPE>
<ACCOUNT_ID NAME="Expense Account 666999">666999</ACCOUNT_ID>
<AMOUNT>3000</AMOUNT>
<DESCRIPTION>Expense2</DESCRIPTION>
<PROJECT_ID NAME="def">456</PROJECT_ID>
<SHARE>30</SHARE>
</POSTING>
<POSTING id="10">
<TYPE>C</TYPE>
<ACCOUNT_ID NAME="Credit Account 444888">444888</ACCOUNT_ID>
<AMOUNT>-10000</AMOUNT>
<DESCRIPTION>Expense2</DESCRIPTION>
</POSTING>
</POSTINGS>
</ACCOUNTING>
</DATA>
在此示例中,转移分配了两个项目:
"abc" having id '123' with a share of 70 %
"def" having id '456' with a share of 30 %
转移包含两个帖子:
"Expense1" with the amount of 1,000
"Expense2" with the amount of 10,000
理想情况下,我需要在给定其份额百分比的情况下总结每个项目的借记(TYPE ='D')过账,并显示带有值的消息。
在上面的示例中,这将导致:
Project "abc" has an amount of 7700
Project "def" has an amount of 3300
我遗漏了税务贴子。如果解决方案可以从总计中排除那些税务过账,那将会很棒。 税务过账将通过以下方式确定:
<ACCOUNT_ID NAME="Tax Account">
AMOUNT字段包含计算的共享发布,这意味着在将其添加到项目的总和之前无需先计算金额。
希望这是有道理的。
2015-03-20更新
谢谢Michael,
该指南让我朝着正确的方向前进。
我让分组工作,现在我可以使用项目名称参考和单个金额输出每个项目的消息,但无法执行金额的总和:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt" extension-element-prefixes="msxml">
<xsl:key name="postings-by-project" match="POSTING" use="PROJECT_ID" />
<xsl:template match="/DATA">
<messages>
<message class="1">
<xsl:for-each select="(ACCOUNTING/POSTINGS/POSTING[count(. | key('postings-by-project', PROJECT_ID)[1]) = 1])">
<xsl:sort select="PROJECT_ID" />
Project "<xsl:value-of select="PROJECT_ID" />" has an amount of
<xsl:for-each select="key('postings-by-project', PROJECT_ID)">
<xsl:sort select="AMOUNT" />
<xsl:if test="(TYPE='D')">
<xsl:if test="not(ACCOUNT_ID/@NAME = 'Tax Account')">
<xsl:value-of select="AMOUNT" />+
</xsl:if>
</xsl:if>
</xsl:for-each>
<BR>
</xsl:for-each>
</message>
</messages>
</xsl:template>
</xsl:stylesheet>
输出:
Project "38305" has an amount of 56+ 595+
Project "70491" has an amount of 24+ 255+
以上是使用实际过账的实际项目进行测试,代码正确地排除了税行。
现在我只需要进行最后的总结。
我已经尝试在for-each POSTING循环中创建变量并在最后位置()中求和()然后只写入第一个Amount:
<xsl:variable name="vAmount"><number><xsl:value-of select="AMOUNT"/></number></xsl:variable>
<xsl:choose>
<xsl:when test="position() = last()">
<message class="1">
<xsl:value-of select="sum(msxml:node-set($vAmount))"/>
</message>
</xsl:when>
</xsl:choose>
答案 0 :(得分:0)
很难确定你究竟是什么,主要是因为你的例子令人困惑。您的第一个示例包含没有PROJECT_ID的POSTING,并且您显示的结果与实际金额不匹配。在任何情况下,使用sum()函数可以轻松完成求和 - 无需任何精心设计。
我建议您查看以下简化示例:
<强> XML 强>
<DATA>
<ACCOUNTING>
<POSTINGS>
<POSTING id="1">
<ACCOUNT_ID NAME="Expense Account 555777">555777</ACCOUNT_ID>
<AMOUNT>700</AMOUNT>
<PROJECT_ID NAME="abc">123</PROJECT_ID>
</POSTING>
<POSTING id="2">
<ACCOUNT_ID NAME="Expense Account 555777">555777</ACCOUNT_ID>
<AMOUNT>300</AMOUNT>
<PROJECT_ID NAME="def">456</PROJECT_ID>
</POSTING>
<POSTING id="6">
<ACCOUNT_ID NAME="Expense Account 666999">666999</ACCOUNT_ID>
<AMOUNT>7000</AMOUNT>
<PROJECT_ID NAME="abc">123</PROJECT_ID>
</POSTING>
<POSTING id="7">
<ACCOUNT_ID NAME="Expense Account 666999">666999</ACCOUNT_ID>
<AMOUNT>3000</AMOUNT>
<PROJECT_ID NAME="def">456</PROJECT_ID>
</POSTING>
<POSTING id="8">
<ACCOUNT_ID NAME="Tax Account">1000001</ACCOUNT_ID>
<AMOUNT>600</AMOUNT>
<PROJECT_ID NAME="def">456</PROJECT_ID>
</POSTING>
</POSTINGS>
</ACCOUNTING>
</DATA>
应用以下样式表:
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:key name="postings-by-project" match="POSTING" use="PROJECT_ID" />
<xsl:template match="/DATA">
<result>
<xsl:for-each select="ACCOUNTING/POSTINGS/POSTING[count(. | key('postings-by-project', PROJECT_ID)[1]) = 1]">
<project name="{PROJECT_ID/@NAME}">
<amounts>
<xsl:for-each select="key('postings-by-project', PROJECT_ID)">
<amount account="{ACCOUNT_ID/@NAME}">
<xsl:value-of select="AMOUNT" />
</amount>
</xsl:for-each>
</amounts>
<total>
<xsl:value-of select="sum(key('postings-by-project', PROJECT_ID)/AMOUNT)" />
</total>
<total-without-tax>
<xsl:value-of select="sum(key('postings-by-project', PROJECT_ID)[not(ACCOUNT_ID/@NAME='Tax Account')]/AMOUNT)" />
</total-without-tax>
</project>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>
将产生以下结果:
<?xml version="1.0" encoding="utf-8"?>
<result>
<project name="abc">
<amounts>
<amount account="Expense Account 555777">700</amount>
<amount account="Expense Account 666999">7000</amount>
</amounts>
<total>7700</total>
<total-without-tax>7700</total-without-tax>
</project>
<project name="def">
<amounts>
<amount account="Expense Account 555777">300</amount>
<amount account="Expense Account 666999">3000</amount>
<amount account="Tax Account">600</amount>
</amounts>
<total>3900</total>
<total-without-tax>3300</total-without-tax>
</project>
</result>
为方便起见,我已将结果输出XML,以便您可以轻松查看内容。