Re: [xsl] Multiple xml file summation

Subject: Re: [xsl] Multiple xml file summation
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sat, 19 Mar 2005 11:09:19 +1100
As simple as this (both in XSLT/XPath 1.0 and 2.0):

   sum(document(/*/*)/*/*/@number)


Cheers,
Dimitre Novatchev.


On Fri, 18 Mar 2005 15:34:46 -0500, Thomas Mitchell, Jr.
<tmitchel@xxxxxxxxxxxx> wrote:
> Hi all,
> 
>    I have found messages in the archive that come close to what I need,
> but I haven't found the answer, so I would like to pose the following
> question.  I would like to sum numbers from multiple xml input files,
> while not knowing in advance how many files there will be or their
> names.  They are contain in another xml file:
> 
> <----- filelist.xml ------->
> <filenames>
>  <filename>multiplefiletest1.xml</filename>
>  <filename>multiplefiletest2.xml</filename>
>  <filename>multiplefiletest3.xml</filename>
> </filenames>
> 
> <----- multiplefiletest1.xml ------->
> <element1>
>  <element2 name="file1" number="1"/>
> </element1>
> 
> <----- multiplefiletest2.xml ------->
> <element1>
>  <element2 name="file2" number="2"/>
> </element1>
> 
> <----- multiplefiletest3.xml ------->
> <element1>
>  <element2 name="file3" number="3"/>
> </element1>
> 
> My stylesheet is processing filelist.xml, and I can cycle through each
> file listed easy enough:
> 
> <xsl:template match="/">
>  <xsl:for-each select="filenames/filename">
>    File name: <xsl:value-of select="."/>
>    <xsl:variable name="newFile" select="document(.)"/>
>    Name attribute inside file: <xsl:value-of
> select="$newFile/element1/element2/@name"/>
>    Number attribute inside file: <xsl:value-of
> select="$newFile/element1/element2/@number"/>
>    <xsl:text>
>    </xsl:text>
>  </xsl:for-each>
>    Total of number attributes: ??????????????????
> </xsl:template>
> 
> <------ output ---------->
>    File name: multiplefiletest1.xml
>    Name attribute inside file: file1
>    Number attribute inside file: 1
> 
>    File name: multiplefiletest2.xml
>    Name attribute inside file: file2
>    Number attribute inside file: 2
> 
>    File name: multiplefiletest3.xml
>    Name attribute inside file: file3
>    Number attribute inside file: 3
> 
>    Total of number attributes: <-----Want "6" here---->
> 
> But coming up with a total for the number attributes is eluding me.  Is
> this a two step process, going through and writing an xml file with all
> nodes in it, then transforming that to get the total?  It seems like
> there should be a way to do this in one pass using sum(), as all the
> values are right there.  Hopefully, I'm just missing something simple...
> 
> Any help is much appreciated.
> Tom Mitchell

Current Thread