#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    XSLT sum funtion problem


    Below is my code

    XML
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="books3.xsl"?>
    <books>
    	<book>
    		<title type='fiction'>Thor</title>
    		<author>Grant, Coleman and Lui</author>
    		<publisher>Swinburne</publisher>
    		<year>2010</year>
    		<price>32</price>
    	</book>
    	<book>
    		<title type='non-fiction'>Something Borrowed</title>
    		<author>Jason</author>
    		<publisher>Deakins</publisher>
    		<year>2011</year>
    		<price>35</price>
    	</book>
    	<book>
    		<title type='fiction'>The Dilemma</title>
    		<author>Sonia and Nicole</author>
    		<publisher>Monash</publisher>
    		<year>2009</year>
    		<price>25</price>
    	</book>
    	<book>
    		<title type='fiction'>Secretariat</title>
    		<author>Harry</author>
    		<publisher>William Angliss</publisher>
    		<year>2007</year>
    		<price>20</price>
    	</book>
    </books>
    XSL
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <html>
      <body>
    	<table border="1">
    		<tr>
    			<th>Title</th>
    			<th>Author</th>
    			<th>Price</th>
    		</tr>
    		<xsl:for-each select="/books/book[title/@type='fiction']">
    		<xsl:if test="price &lt; 30">
    		<tr>
    			<td><xsl:value-of select="title"/></td>
    			<td><xsl:value-of select="author"/></td>
    			<td><xsl:value-of select="price"/></td>
    		</tr>
    		</xsl:if>
        		</xsl:for-each>
    		<tr>
    			<td colspan="3"><xsl:value-of select="sum(/books/book[title/@type='fiction']/price)" /></td>
    		</tr>
      	</table>
      </body>
      </html>
    </xsl:template>
    
    </xsl:stylesheet>
    What I've been able to do up to now is retrieve all books with type fiction and price less than 30 and display their title, author and price. What i can't do is to sum up the price. Can anyone help me please?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    please not use for-each for little exsample

    xsl:param as variable to modify xsl result

    better think xsl
    xsl
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output indent="yes" method="html"/>
    	<xsl:param name="kind_of" select="'fiction'"/>
    	<xsl:param name="borderprice" select="30"/>
    	<xsl:template match="/">
    		<html>
    			<style><![CDATA[
    table{
    border: 2px solid black;
    }
    th,td{
    border: 2px solid black;
    }
    tr{
    text-align: center;
    }
    tr.head{
    text-align: center;
    background-color:#ff0000;
    }
    tr.odd{
    background-color:#ff9900;
    }
    tr.even{
    background-color:#ff0099;
    }
    tr.sum{
    background-color:#aa0099;
    text-align: center;
    }
    ]]>
    			</style>
    			<body>
    				<table border="1">
    					<tr class="head">
    						<th>Title</th>
    						<th>Author</th>
    						<th>Price</th>
    					</tr>
    					<xsl:apply-templates select="books/book[title/@type =$kind_of][price &amp;lt; $borderprice]"/>
    					<tr class="sum">
    						<td colspan="2">
    							<xsl:text>&#xA0;</xsl:text>
    						</td>
    						<td>
    							<xsl:value-of select="sum(books/book[title/@type =$kind_of][price &amp;lt; $borderprice]/price)"/>
    						</td>
    					</tr>
    				</table>
    			</body>
    		</html>
    	</xsl:template>
    	<xsl:template match="book">
    		<tr>
    			<xsl:attribute name="class">
    				<xsl:choose>
    					<xsl:when test="position() mod 2 = 1">odd</xsl:when>
    					<xsl:otherwise>even</xsl:otherwise>
    				</xsl:choose>
    			</xsl:attribute>
    			<td>
    				<xsl:value-of select="title"/>
    			</td>
    			<td>
    				<xsl:value-of select="author"/>
    			</td>
    			<td>
    				<xsl:value-of select="price"/>
    			</td>
    		</tr>
    	</xsl:template>
    </xsl:stylesheet>
    result html code with css
    Code:
    <html>
      <style>
    table{
    border: 2px solid black;
    }
    th,td{
    border: 2px solid black;
    }
    tr{
    text-align: center;
    }
    tr.head{
    text-align: center;
    background-color:#ff0000;
    }
    tr.odd{
    background-color:#ff9900;
    }
    tr.even{
    background-color:#ff0099;
    }
    tr.sum{
    background-color:#aa0099;
    text-align: center;
    }
    
    			
      </style>
      <body>
        <table border="1">
          <tr class="head">
            <th>Title</th>
            <th>Author</th>
            <th>Price</th>
          </tr>
          <tr class="odd">
            <td>The Dilemma</td>
            <td>Sonia and Nicole</td>
            <td>25</td>
          </tr>
          <tr class="even">
            <td>Secretariat</td>
            <td>Harry</td>
            <td>20</td>
          </tr>
          <tr class="sum">
            <td colspan="2">&nbsp;</td>
            <td>45</td>
          </tr>
        </table>
      </body>
    </html>
    Helmut Hagemann Germany

    fallen to the bottom of the facts?
    I reach my hand and we go together


    wer lesen und google kann ist klar im Vorteil
    who read and google is able is clear in the advantage

IMN logo majestic logo threadwatch logo seochat tools logo