Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
XSLT sum by attribute
Old 04-04-2010, 11:46 PM XSLT sum by attribute
Extreme Talker

Posts: 247
Trades: 3
Greetings-

I am formatting a receipt-style page, and want to do a sum by attribute. The XML looks like this:

Code:
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<report>
    <entity>
            <row>
                <item thename="product1" theval="100"></some_value>
                <item thename="product2" theval="200"></some_value>
                <item thename="product1" theval="1"></some_value>
                <item thename="product2" theval="4"></some_value>
                <item thename="product2" theval="5"></some_value>
            </row>
    </entity>
</report>
and I would like the output to look like this:

product1: 101
product2: 209


Can you help me with the style.xsl?

Thanks in advance.
__________________

Please login or register to view this content. Registration is FREE
CouponGuy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-05-2010, 08:47 AM Re: XSLT sum by attribute
chrishirst's Avatar
Defies a Status

Posts: 44,050
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
XSL is NOT a scripting language so has no mathematical capabilities
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-05-2010, 09:22 AM Re: XSLT sum by attribute
badams's Avatar
Skilled Talker

Latest Blog Post:
Dum Skype Firefox plugin
Posts: 69
Name: Barry Adams
Location: London
Trades: 0
XSLT does allow some basic mathes in the "value-of" attribute.
__________________

Please login or register to view this content. Registration is FREE
@
Please login or register to view this content. Registration is FREE
(320+ Subjects)
badams is offline
Reply With Quote
View Public Profile Visit badams's homepage!
 
Old 04-05-2010, 09:49 AM Re: XSLT sum by attribute
chrishirst's Avatar
Defies a Status

Posts: 44,050
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Originally Posted by badams View Post
XSLT does allow some basic mathes in the "value-of" attribute.
That is not mathematics, it is a method of selecting elements according to an attribute value.

XSL cannot ADD the value of those attributes to a running total.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-05-2010, 11:20 AM Re: XSLT sum by attribute
voxink's Avatar
Novice Talker

Posts: 13
Name: Adrian
Trades: 0
XML
Code:
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<report>
    <entity>
            <row>
                <item thename="product1" theval="100"></item>
                <item thename="product2" theval="200"></item>
                <item thename="product1" theval="1"></item>
                <item thename="product2" theval="4"></item>
                <item thename="product2" theval="5"></item>
            </row>
    </entity>
</report>
XSL
Code:
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="report">
        product1: <xsl:value-of select="sum(entity/row/item[@thename='product1']/@theval)"/>
        product2: <xsl:value-of select="sum(entity/row/item[@thename='product2']/@theval)"/>
    </xsl:template>
    
</xsl:stylesheet>
OUTPUT
product1: 101
product2: 209
voxink is offline
Reply With Quote
View Public Profile
 
Old 04-05-2010, 12:02 PM Re: XSLT sum by attribute
chrishirst's Avatar
Defies a Status

Posts: 44,050
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Good point!

Using XPath expressions.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-05-2010, 03:10 PM Re: XSLT sum by attribute
Extreme Talker

Posts: 247
Trades: 3
Thanks, everybody, for your help. Voxink, that returns exactly what I asked for!

I've been toying with for-each to try to get this to be dynamic. That is, if I apply this stylesheet to another XML document with 4 products, it will display the sum of each of the 4 products.

It seems that this works so far:
Code:
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="report">	
		<xsl:for-each select="entity/row/item/@thename[not(.=preceding::item/@thename)]">
			<xsl:value-of select="current()"/>=
			<xsl:value-of select="sum(/report/entity/row/item[@thename=current()]/@theval)"/><br />
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
Again, thanks for your help! It was a huge push in the right direction. Without it, I would still be sleeping through the tutorial lessons online.
__________________

Please login or register to view this content. Registration is FREE
CouponGuy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to XSLT sum by attribute
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27220 seconds with 11 queries