RE: [xsl] Multiple xml file summation

Subject: RE: [xsl] Multiple xml file summation
From: "Thomas Mitchell, Jr." <tmitchel@xxxxxxxxxxxx>
Date: Mon, 21 Mar 2005 09:15:23 -0500
I'm sure it isn't said enough, so I will say it now: thanks to all who
contribute to the list.  Your input is much appreciated by those
learning/experimenting with this interesting technology.

<xsl:value-of
select="sum(document(/filenames/filename)/element1/element2/@number)"/>

works great.  I boxed myself into thinking of this as a problem within a
for-each loop.  This list has removed the chains from my narrow train of
thought.


Thank you for your time, Dimitre.
Tom Mitchell


-----Original Message-----
From: Dimitre Novatchev [mailto:dnovatchev@xxxxxxxxx]
Sent: Friday, March 18, 2005 7:09 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Multiple xml file summation

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