XLST sum

Being hired to work on an extensive, complex and most of all, very used system, I naturally have to jump between different sides of the system. Having added some features to an application I then got back to XSLT.

One of the things I had to do was to generate a sum. Now, I could have done this in Lansa, but it was more logically done in XSLT so I did that. Except, since I could not remember the exact syntax I did a search and found many convoluted examples. And it is actually quite easy:

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

Specifically I used it in this context:

<xsl:template match="/lxml:data/lxml:lists/lxml:list[@name='ESTLIST']">
<xsl:variable name="thelist"
 select="/lxml:data/lxml:lists/lxml:list[@name='ESTLIST']" />
...
<xsl:for-each select="$thelist/lxml:list-entries/lxml:entry">
 <xsl:variable name="BUTNR" select="lxml:column[@name='X_BUTNR']" />
 <xsl:variable name="BUKORT" select="lxml:column[@name='X_BUKORT']" />
 <xsl:variable name="EANTAL" select="lxml:column[@name='X_EANTAL']" /
...
</xsl:for-each>
...
<xsl:value-of select="sum($thelist/lxml:list-entries/lxml:entry/lxml:column[@name='X_EANTAL'])" />
...
</xsl:template>

The XML source was essentially

<?xml version="1.0"?>
 <lxml:data xmlns:lxml="http://www.lansa.com/2002/XML/Runtime-Data">
 <lxml:lists>
 <lxml:list name="ESTLIST" row-count="3">
 <lxml:list-header>
 <lxml:header name="X_BUTNR">...</lxml:header>
 <lxml:header name="X_BUKORT">...</lxml:header>
 <lxml:header name="X_EANTAL">...</lxml:header>
 </lxml:list-header>
 <lxml:list-entries>
 <lxml:entry>
 <lxml:column name="X_BUTNR" id="ESTLIST.0001.X_BUTNR">ABCD</lxml:column>
 <lxml:column name="X_BUKORT" id="ESTLIST.0001.X_BUKORT">aAbBcCdDeEfFgGh</lxml:column>
 <lxml:column name="X_EANTAL" id="ESTLIST.0001.X_EANTAL">12345</lxml:column>
 </lxml:entry>
 <lxml:entry>
 <lxml:column name="X_BUTNR" id="ESTLIST.0002.X_BUTNR">ABCD</lxml:column>
 <lxml:column name="X_BUKORT" id="ESTLIST.0002.X_BUKORT">aAbBcCdDeEfFgGh</lxml:column>
 <lxml:column name="X_EANTAL" id="ESTLIST.0002.X_EANTAL">12345</lxml:column>
 </lxml:entry>
 <lxml:entry>
 <lxml:column name="X_BUTNR" id="ESTLIST.0003.X_BUTNR">ABCD</lxml:column>
 <lxml:column name="X_BUKORT" id="ESTLIST.0003.X_BUKORT">aAbBcCdDeEfFgGh</lxml:column>
 <lxml:column name="X_EANTAL" id="ESTLIST.0003.X_EANTAL">12345</lxml:column>
 </lxml:entry>
 </lxml:list-entries>
 </lxml:list>
 </lxml:lists>
 </lxml:data>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s