Re: [xsl] Problem with Sum function

Subject: Re: [xsl] Problem with Sum function
From: Jon Gorman <jonathan.gorman@xxxxxxxxx>
Date: Mon, 22 Aug 2005 14:56:11 -0500
On 8/22/05, Meena Nanjundeswar <meenasargur@xxxxxxxxx> wrote:
> Well, the scenario is a little complicated. I have a stylesheet A with
> an xml file A. Now, inside stylesheets A, I am displaying some
> elements from an external XML file B, based on some values in
> stylesheet A.
>
> Eg:
> XML A:
>
> <root>
>                <nodes>
>                        <node>
>                                <node--num>123</node-num>
>                                <child-nodes>
>                                        <child-node-num>456</child-node-num>
>        <child-node-num>789</child-node-num>
>
>
>                                </child-nodes>
>                        </node>
>                        </nodes>
>                        </root>
>
> Now, my external xml file B has some additional values based on the
> node-num and child-node-num from XML file A.
>
> Eg: XML B would be like this:
>
> <Bnode>
>     <Bnode-num>123</Bnode-num>
>             <Bchild-nodes1>
>                      <Bchild-node1>
>                                 <Bchild-node-num>456</Bchild-node-num>
>                                  <Bchild-node-value>22</Bchild-node-value>
>                      <Bchild-node1>
>                        <Bchild-node1>
>                                 <Bchild-node-num>789</Bchild-node-num>
>                                  <Bchild-node-value>29</Bchild-node-value>
>                      <Bchild-node1>
>
>             </Bchildnodes1>
>
> </Bnode>
>
> Here is what I am doing in my stylesheet A. Now, I have some external
> server calls made to match the node-num and child-node-num from XML A
> with the correspondng values on XML B and display the
> sum(Bchild-node-value).
>
>        <xsl:template name="Bnode">
>                <xsl:param name="mode"/>
>                <xsl:for-each select="/root/nodes/node[child-nodes]">
>                        <xsl:variable name="node-num">
>                                <xsl:value-of select="node-num"/>
>                        </xsl:variable>
>                        <xsl:for-each select="./child-nodes/child-node-num">
>                                <xsl:variable name="child-node-num">
>                                        <xsl:value-of select="."/>
>                                </xsl:variable>
>                                <xsl:apply-templates select="<!--This is
servercall-->">
>                                                        <!--
</xsl:for-each>
>                </xsl:for-each>
>                        </xsl:template>
>        <xsl:template match="Bchild-node1">
>
>                <xsl:value-of select="sum(Bchild-node-value)"/>
>
>        </xsl:template>

In my other email I mentioned that this was your issue.  You're
getting the sum of all the Bchild-node-value for each Bchild-node1.
But what you really want is the sum of all Bchild-node-value for each
Bchild-nodes1.  There seems to be the added condition that the value
has a sibling with the proper num.

To rephrase it in another more general pesuedo format, your xslt code says
this:

For each node called Bchild-node1 get the sum of all it's value nodes.

Which of course prints out one value of each Bchild-node1.  So either
you need to sum the results of that apply-templates, or better yet
just sum it up in the first place.

Your example code was pretty broken and I wasn't sure exactly how the
document was put together, let alone what the "server calls" were.
Are you using the document?  But assuming that your doing something
similar to the document calls I would suggest two steps:


1) Get all the nodes that you want (ie ones that subnodes can be found
in this source document) and put them in a variable:

<xsl:variable name="sumNodes"
select="Bchild-nodes1/Bchild-node1[Bchild-node-num =
/root/nodes/node/child-nodes/child-node-num]" />

2) Get the sum of the appropriate values

<xsl:value-of select="sum($sumNodes/Bchild-node-value)" />

It's hard to tell the details, and to tell the truth I'm to busy to do
much more.

Perhaps post a valid example similar to the above and make it a litle
more clear what is being imported and what is not.

Jon Gorman









>
> Hope this is clear. Please let me know if there is any solution.
>
> Thank you for all your help.
>
> Meena
>
> On 8/22/05, Mukul Gandhi <gandhi.mukul@xxxxxxxxx> wrote:
> > Please try
> >
> > <xsl:template match="/node1">
> >   <xsl:value-of select="sum(childnode1a/childnode11a/value)" />
> > </xsl:template>
> >
> > Regards,
> > Mukul
> >
> > On 8/22/05, Meena Nanjundeswar <meenasargur@xxxxxxxxx> wrote:
> > > Hi:
> > >
> > > My xml file looks like this:
> > >
> > > <node1>
> > >      <childnode1a>
> > >             <childnode11a>
> > >                     <value></value>
> > >               </childnode11a>
> > >       </childnode1a>
> > > </node1>
> > >
> > > There can be any number of <childnode11a> elements containing <value>.
> > > Now, I am trying to compute the sum(value). Instead of giving me a
> > > total sum of all the rows, it display the values individually. I
> > > believe we have to use recursion. Can anyone please help me with this
> > > problem?
> > >
> > > Thanks
> >
> >
>
>
> --
> NorthWest Airlines
> Office: 612-726-0793

Current Thread