File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes how can I efficiently  use sum() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "how can I efficiently  use sum()" Watch "how can I efficiently  use sum()" New topic
Author

how can I efficiently use sum()

Fred Fasle
Greenhorn

Joined: Oct 22, 2003
Posts: 3
Hello there,
I have an application that generates the XML below. I can sum the values and generate HTML under each brand of shoe by using.
<xsl:template match="shoes[@name='Nike']">
<td align="center"><xsl:value-of select="@name"/></td>
<td><xsl:value-of select="sum(//shoes[@name='Nike']/*)"/></td>
<xsl:apply-templates/>
</xsl:template>
But is there a way I can write this code so that when I add another ten different brands of shoe I don't have to repeat the code ten times replacing the name each time.
Thanks for any ideas
<product>
<shoes name="Nike">
<sold>10</sold>
<onorder>15</onorder>
<instore>6</instore>
</shoes>
<shoes name="Reebok">
<sold>7</sold>
<onorder>13</onorder>
<instore>9</instore>
</shoes>
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
You may want to use xsl:key to get unique brand names and set up a variable called something like $brand. Then use this variable as a parm to call the template code you have.


Tony Yan<br /> <br />IBM Certified Developer XML and Related Technology<br />Sun Certified Web Component Developer For J2EE Platform<br />Sun Certified Programmer For Java 2 Platform
Fred Fasle
Greenhorn

Joined: Oct 22, 2003
Posts: 3
Any chance of a little more detail. I'm not very good at this. Or a link to tutorial explaining it.
Thanx
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
I assume your xml is like this:

With this xsl:

You can get tabulated shoe info. I highly recommend using xsl:key if your xml is not too big. key is good and fast but memory intensive.
Hope it helps.
Also, Muenchian Method
 
 
subject: how can I efficiently use sum()
 
Similar Threads
possible to create dynamically named nodes via xslt?
XSLT and embedded tags in text.
splitting xsl file :creating dynamically
Xalan problem
Removing nodes based on a element