XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 28th, 2011, 04:17 AM
jason_moutia jason_moutia is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 24 jason_moutia New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 6 h 55 m 3 sec
Reputation 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 < 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?

Reply With Quote
  #2  
Old April 29th, 2011, 11:40 AM
xml-profi xml-profi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 190 xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 7 h 8 m 25 sec
Reputation Power: 48
Send a message via ICQ to xml-profi
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 &lt; $borderprice]"/>
					<tr class="sum">
						<td colspan="2">
							<xsl:text>*</xsl:text>
						</td>
						<td>
							<xsl:value-of select="sum(books/book[title/@type =$kind_of][price &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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSLT sum funtion problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap