Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 1st, 2005, 02:02 PM
Registered User
 
Join Date: Jul 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Doing addition on Unique Id's

I have got part of the problem working, but summation seems to be going wrong. I am getting the counts( no of books sold), but when I am trying to add them it just gets concatenated. Please help me on this issue. Thanks

Required Output:
*****************
JavaScript 64
Photoshop 6
*****************

Current Output:
*****************
JavaScript 2359
Photoshop 6
*****************

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="stylesheet10.xsl"?>
   <bookstore>
     <book>
       <counts>2</counts>
       <title>JavaScript</title>
     </book>
     <book>
      <counts>3</counts>
      <title>JavaScript</title>
     </book>
     <book>
       <counts>6</counts>
       <title>Photoshop</title>
     </book>
      <book>
       <counts>59</counts>
       <title>JavaScript</title>
     </book>
   </bookstore>

XSLT:

<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:key name="kbook" match="book" use="title"/>
<xsl:key name="crount" match="book" use="counts"/>

         <xsl:template match="/">
           <table>
             <xsl:apply-templates select="node()|@*"/>
           </table>
         </xsl:template>

   <xsl:template match="book">
    <xsl:if test="generate-id()=generate-id(key('kbook',title)[1])">
           <tr>
             <td>
                <xsl:value-of select="title"/>
             </td>

             <td>
          <xsl:for-each select="key('kbook', title)">
            <xsl:value-of select="sum(counts)"/>
        </xsl:for-each>
             </td>
           </tr>

         </xsl:if>
         </xsl:template>

     <xsl:template match="text()"/>

</xsl:stylesheet>

  #2 (permalink)  
Old August 1st, 2005, 02:45 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This:

<xsl:for-each select="key('kbook', title)">
            <xsl:value-of select="sum(counts)"/>
        </xsl:for-each>


says "for each of the books with this title, add up all the <counts> elements for this book and output the total. There's only one <counts> for each book, so that's the value you get.

What you want is a single value, not one for each book:

<xsl:value-of select="key('kbook', title)/counts"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference


Similar Threads
Thread Thread Starter Forum Replies Last Post
list session id's and visit time samwisegamgee16 Classic ASP Basics 4 August 23rd, 2007 02:32 AM
Overlapping id's problem Gert SQL Server 2005 4 May 26th, 2006 03:55 AM
Underscores in ID's?????? matallen XSLT 0 March 6th, 2006 02:04 PM
Recording User ID's Figgis Access 3 October 17th, 2005 01:18 PM
Chapter 12 div id's buzzuh BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 September 3rd, 2004 08:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.