XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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 May 12th, 2004, 08:13 AM
Wainson Wainson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 2 Wainson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question noobie question - sum of function of child nodes?

Hi,
I have an xml document which is basically of this form:

Code:
<list>
    <cd artist="Various" name="humming sounds">
        <track name="fridge" length="5:10">
        <track name="computer" length="1:19">
        <track name="transformer" length="2:00">
        <track name="air conditioner" length="9:15">
    </cd>
    <cd artist="Various" name="buzzing sounds">
        <track name="chainsaw" length="5:10">
        <track name="electric toothbrush" length="1:19">
        <track name="car alarm" length="29:15">
    </cd>
    {many more CD elements with many more track elements}
</list>


and I would like to be able to add an attribute to each cd element which contains the total length (in seconds) of all the tracks inside it... I already have the code to make a table of the cds, but all I need to be able to do now is to make a column on the table containing the total lengths...

I've tried making a new <xsl:variable in each track name containing the length in seconds of each track, but I can't use the sum function on all the variables when in the scope of the cd (even when using "sum(track/length_in_seconds)").

I also tried using the sum function on the processed lengths directly, but I don't know the syntax for this...

Does anybody know how I can do this? I've searched on w3schools and on LOADS of sites with google, but I've still not managed to come up with anything!!!

<edit> I didn't know if this should have gone in the XML forum or the beginner programming forum, sorry if this is the wrong place! </edit>

Last edited by Wainson : May 12th, 2004 at 08:15 AM.

Reply With Quote
  #2  
Old May 12th, 2004, 06:44 PM
fpmurphy fpmurphy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: USA
Posts: 289 fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 22 m 4 sec
Reputation Power: 6
The xslt sum() function cannot handle the syntax you are using
for minutes and seconds i.e. mm:ss. It simply returns NaN (not
a number)

Here is one way of achieving what you want:

Code:
<?xml version="1.0"?>
<list>
    <cd artist="Various" name="humming sounds">
        <track name="fridge" length="310" />
        <track name="computer" length="79" />
        <track name="transformer" length="120" />
        <track name="air conditioner" length="555" />
    </cd>
    <cd artist="Various" name="buzzing sounds">
        <track name="chainsaw" length="310" />
        <track name="electric toothbrush" length="79" />
        <track name="car alarm" length="1775" />
    </cd>
</list>


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

<xsl:template match="//cd">
     <xsl:variable name="len" select="sum(track/@length)" />
     Length: <xsl:value-of select="floor($len div 60)" />:<xsl:value-of select="$len mod 60" /> 
</xsl:template>

</xsl:stylesheet>

Reply With Quote
  #3  
Old May 13th, 2004, 09:18 AM
Wainson Wainson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 2 Wainson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,
Thanks for the reply, but I was hoping to be able to keep the xml file with the cd listings in it's original state, without converting the times into seconds.

Do you know how I'd be able to add an attribute which would convert the times into seconds which I could then access from the scope of each CD template using the sum function?

Reply With Quote
  #4  
Old May 13th, 2004, 06:48 PM
fpmurphy fpmurphy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: USA
Posts: 289 fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 22 m 4 sec
Reputation Power: 6
Sorry, but you are probably out of luck unless you
write your own template/function. If you convert
the length attribute to the XML schema duration
datatype you could use one of the EXSLT functions
(www.exslt.org) to achive what you want.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > noobie question - sum of function of child nodes?


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT