• Post Reply Bookmark Topic Watch Topic
  • New Topic

how can I efficiently use sum()  RSS feed

 
Fred Fasle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fred Fasle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!