|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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>
|
|
#3
|
|||
|
|||
|
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? |
|
#4
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > noobie question - sum of function of child nodes? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|