0

I need to calculate sums on postings to an accounting system taking into consideration their might be multiple projects assigned on the transfer with different shares of the totals.

Below is a simplified snippet of my 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>

In this example the transfer has assigned two projects:

"abc" having id '123' with a share of 70 %
"def" having id '456' with a share of 30 %

The transfer consists of two postings:

"Expense1" with the amount of 1,000
"Expense2" with the amount of 10,000

Ideally I need to summarize the debit (TYPE='D') postings per project given their share percents, and show a message with the values.

In above example this would result in:

Project "abc" has an amount of 7700
Project "def" has an amount of 3300

I have left out tax postings. Would be great if the solution could exclude those tax posting from the totals. Tax postings will be identified by:

<ACCOUNT_ID NAME="Tax Account">

The AMOUNT fields contains the calculated shared posting, meaning there's no need to first calculate the amount before adding it to the project's total sum.

Hope this makes sense.



Update 2015-03-20

Thanks Michael,

That guide put me in the right direction.

I got the grouping to work, and am now able to output messages per project with the project name reference and the individual amounts, but not able to perform the summation of the amounts:

<?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>
                        &lt;BR&gt;
                </xsl:for-each>
            </message>
        </messages>
    </xsl:template>
</xsl:stylesheet>

This outputs:

Project "38305" has an amount of 56+ 595+ 
Project "70491" has an amount of 24+ 255+ 

Above was tested with actual projects with actual postings, and the code correctly excludes the tax lines.

Now I just need to perform final summation.

I've tried inside the for-each POSTING loop to create variables and sum() them in last position(), but then only the first Amount is written:

<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>
2

1 Answer 1

0

It is quite difficult to figure what exactly you're after, mainly because your examples are confusing. Your first example has POSTINGs without PROJECT_ID, and the results you show don't match the actual amounts. In any case, summing can be done easily by using the sum() function - no need for anything elaborate.

I suggest you look at the following simplified example:

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>

Applying the following stylesheet:

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>

will yield the following result:

<?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>

For convenience, I have made the result output XML, so that you can easily see what is what.

1
  • Thank you very much Michael. Your leads and examples gave me the knowledge to solve the problem.
    – Jack Storm
    Apr 1, 2015 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.