[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Sum of identical nodes

Subject: Re: Sum of identical nodes
From: "Joris Gillis" <roac@xxxxxxxxxx>
Date: Wed, 20 Jul 2005 11:35:20 +0200
xml sum
Hi again,

Tempore 08:18:34, die 07/20/2005 AD, hinc in xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Alvin Ng <ngkwangming@xxxxxxxxx>:

Each file in File1 and File2 have few hundreds nodes of <moid></moid>
and <mt></mt>. My biggest hindrance is how to recursively sum up each
node from each nodeset.


Here you have a solution that works in more general cases (example below). At first sight, the code looks rather bloated and unintellegible. I fear those impressions will remain and 'cpu-intense' will join them:p . but at least it works...

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


<xsl:param name="file1" select="/"/>
<xsl:param name="file2" select="document('file2.xml')"/>

<xsl:template match="/">
<xml>
<xsl:apply-templates select="$file1/xml/moid | $file2/xml/moid" mode="merge"/>
</xml>
</xsl:template>


<xsl:template match="moid" mode="merge">
<xsl:variable name="moid1" select="$file1/xml/moid[.=current()]"/>
<xsl:variable name="moid2" select="$file2/xml/moid[.=current()]"/>
<xsl:if test="count($moid1|$moid2)=1 or (count($moid1|$moid2)=2 and count(.|$moid1)=1)">
<xsl:copy-of select="."/>
<xsl:variable name="mt1" select="$moid1/following::mt[preceding::moid[1]=current()]"/>
<xsl:variable name="mt2" select="$moid2/following::mt[preceding::moid[1]=current()]"/>
<xsl:apply-templates select="$mt1 | $mt2" mode="merge">
<xsl:with-param name="mt1" select="$mt1"/>
<xsl:with-param name="mt2" select="$mt2"/>
<xsl:with-param name="moid" select="."/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>


<xsl:template match="mt" mode="merge">
<xsl:param name="moid"/>
<xsl:param name="mt1"/>
<xsl:param name="mt2"/>
<xsl:variable name="cnt" select="count(preceding::*[preceding::moid=$moid]) + 1"/>
<xsl:variable name="thismt1" select="$mt1[$cnt]"/>
<xsl:variable name="thismt2" select="$mt2[$cnt]"/>
<xsl:if test="count($thismt1|$thismt2)=1 or (count($thismt1|$thismt2)=2 and count(.|$thismt1)=1)">
<xsl:copy><xsl:value-of select="sum($thismt1|$thismt2)"/></xsl:copy>
</xsl:if>
</xsl:template>


</xsl:stylesheet>


Example:


File 1:
<xml>
<moid> AAA </moid>
    <mt> 1 </mt>
    <mt> 2 </mt>
<moid> BBB </moid>
    <mt> 2 </mt>
    <mt> 3 </mt>
	<mt> 8 </mt>
<moid> CCC </moid>
    <mt> 4 </mt>
    <mt> 6 </mt>
</xml>

File 2:
<xml>
<moid> BBB </moid>
    <mt> 0 </mt>
    <mt> 0 </mt>
<moid> AAA </moid>
    <mt> 2 </mt>
    <mt> 3 </mt>
	<mt> 10 </mt>
<moid> DDD </moid>
    <mt> 4 </mt>
    <mt> 4 </mt>
</xml>

Output:
<xml>
	<moid> AAA </moid>
	<mt>3</mt>
	<mt>5</mt>
	<mt>10</mt>
	<moid> BBB </moid>
	<mt>2</mt>
	<mt>3</mt>
	<mt>8</mt>
	<moid> CCC </moid>
	<mt>4</mt>
	<mt>6</mt>
	<moid> DDD </moid>
	<mt>4</mt>
	<mt>4</mt>
</xml>


regards, -- Joris Gillis (http://users.telenet.be/root-jg/me.html) "N N1N;N.N8N5N9N1 N:N1N9 ON? N;N,N4N9 ON,N=ON1 N2N3N1N/N=N?ON= N1OO ON,N=O "

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.