Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > An XML question - calculating time total

Reply
Thread Tools

An XML question - calculating time total

 
 
T-Narg
Guest
Posts: n/a
 
      03-01-2005
I would like to produce the following output based on my XML file:

My Album (2005)
Elapsed Time (hh:mm:ss): 00:07:00

Song 1: title1
Length (hh:mm:ss): 00:02:30

Song 2: title2
Length (hh:mm:ss): 00:02:15

Song 3: title3
Length (hh:mm:ss): 00:02:15


=====

<album>
<general>
<title>My Album</title>
<year>2005</year>
</general>

<content>
<song>
<songTitle>title1</songTitle>
<songLengthInSeconds>150</songLengthInSeconds>
</song>
<song>
<songTitle>title2</songTitle>
<songLengthInSeconds>135</songLengthInSeconds>
</song>
<song>
<songTitle>title3</songTitle>
<songLengthInSeconds>135</songLengthInSeconds>
</song>
</content>
</album>

=====

I need some help in designing the XSLT file. I'd like the
<songLengthInSeconds> to be formatted in hh:mm:ss format. Also, I want to
display the elapsed time (in hh:mm:ss format) based on a total of
<songLengthInSeconds>. Could I perform this summation inside the XSLT?

Thank you very much!



 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      03-01-2005


$ saxon time.xml time.xsl


Elapsed Time (hh:mm:ss): 00:07:00



Song 1: title1
Length (hh:mm:ss): 00:02:30

Song 2: title2
Length (hh:mm:ss): 00:02:15

Song 3: title3
Length (hh:mm:ss): 00:02:15




<album>
<general>
<title>My Album</title>
<year>2005</year>
</general>

<content>
<song>
<songTitle>title1</songTitle>
<songLengthInSeconds>150</songLengthInSeconds>
</song>
<song>
<songTitle>title2</songTitle>
<songLengthInSeconds>135</songLengthInSeconds>
</song>
<song>
<songTitle>title3</songTitle>
<songLengthInSeconds>135</songLengthInSeconds>
</song>
</content>
</album>



<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">



<xslutput method="text"/>

<xsl:template name="time">
<xslaram name="s" select="songLengthInSeconds"/>
<xsl:text>(hh:mm:ss): </xsl:text>
<xsl:variable name="h" select="floor($s div 3600)"/>
<xsl:value-of select="format-number($h,'00')"/>
<xsl:text>:</xsl:text>
<xsl:variable name="m" select="floor(($s - $h * 60) div 60)"/>
<xsl:value-of select="format-number($m,'00')"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="format-number($s - $h*3600 - $m*60,'00')"/>
</xsl:template>

<xsl:template match="song">
Song <xsl:number/>: <xsl:value-of select="songTitle"/>
Length <xsl:call-template name="time"/>
</xsl:template>


<xsl:template match="general">
<xsl:value-of select="Title"/>
Elapsed Time <xsl:text/>
<xsl:call-template name="time">
<xsl:with-param name="s" select="sum(../content/song/songLengthInSeconds)"/>
</xsl:call-template>
</xsl:template>

</xsl:stylesheet>

 
Reply With Quote
 
 
 
 
Mukul Gandhi
Guest
Posts: n/a
 
      03-01-2005
Please try this XSL..

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

<xslutput method="text"/>

<xsl:template match="/album">
<xsl:apply-templates select="general" />
<xsl:apply-templates select="content/song" />
</xsl:template>

<xsl:template match="general">
<xsl:value-of select="title" /> (<xsl:value-of select="year"
/>)<xsl:text>&#xA;</xsl:text>
<xsl:variable name="elapsed-time">
<xsl:call-template name="format-time">
<xsl:with-param name="x"
select="sum(following-sibling::content[1]/song/songLengthInSeconds)"
/>
</xsl:call-template>
</xsl:variable>
Elapsed Time (hh:mm:ss): <xsl:value-of select="$elapsed-time"
/><xsl:text>&#xA;</xsl:text>
</xsl:template>

<xsl:template match="content/song">
Song 1: <xsl:value-of select="songTitle"
/><xsl:text>&#xA;</xsl:text>
<xsl:variable name="length">
<xsl:call-template name="format-time">
<xsl:with-param name="x" select="songLengthInSeconds" />
</xsl:call-template>
</xsl:variable>
Length (hh:mm:ss): <xsl:value-of select="$length"
/><xsl:text>&#xA;</xsl:text>
</xsl:template>

<xsl:template name="format-time">
<xslaram name="x" />

<xsl:value-of select="format-number(floor(($x div 60) div 60),'00')"
/>:<xsl:value-of select="format-number(floor(($x div 60) mod
60),'00')" />:<xsl:value-of select="format-number($x mod 60,'00')" />
</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

"T-Narg" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> I would like to produce the following output based on my XML file:
>
> My Album (2005)
> Elapsed Time (hh:mm:ss): 00:07:00
>
> Song 1: title1
> Length (hh:mm:ss): 00:02:30
>
> Song 2: title2
> Length (hh:mm:ss): 00:02:15
>
> Song 3: title3
> Length (hh:mm:ss): 00:02:15
>
>
> =====
>
> <album>
> <general>
> <title>My Album</title>
> <year>2005</year>
> </general>
>
> <content>
> <song>
> <songTitle>title1</songTitle>
> <songLengthInSeconds>150</songLengthInSeconds>
> </song>
> <song>
> <songTitle>title2</songTitle>
> <songLengthInSeconds>135</songLengthInSeconds>
> </song>
> <song>
> <songTitle>title3</songTitle>
> <songLengthInSeconds>135</songLengthInSeconds>
> </song>
> </content>
> </album>
>
> =====
>
> I need some help in designing the XSLT file. I'd like the
> <songLengthInSeconds> to be formatted in hh:mm:ss format. Also, I want to
> display the elapsed time (in hh:mm:ss format) based on a total of
> <songLengthInSeconds>. Could I perform this summation inside the XSLT?
>
> Thank you very much!

 
Reply With Quote
 
T-Narg
Guest
Posts: n/a
 
      03-03-2005
Works well! Thank you very much.

"Mukul Gandhi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Please try this XSL..
>
> <?xml version="1.0"?>
> <xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
>
> <xslutput method="text"/>
>
> <xsl:template match="/album">
> <xsl:apply-templates select="general" />
> <xsl:apply-templates select="content/song" />
> </xsl:template>
>
> <xsl:template match="general">
> <xsl:value-of select="title" /> (<xsl:value-of select="year"
> />)<xsl:text>&#xA;</xsl:text>
> <xsl:variable name="elapsed-time">
> <xsl:call-template name="format-time">
> <xsl:with-param name="x"
> select="sum(following-sibling::content[1]/song/songLengthInSeconds)"
> />
> </xsl:call-template>
> </xsl:variable>
> Elapsed Time (hh:mm:ss): <xsl:value-of select="$elapsed-time"
> /><xsl:text>&#xA;</xsl:text>
> </xsl:template>
>
> <xsl:template match="content/song">
> Song 1: <xsl:value-of select="songTitle"
> /><xsl:text>&#xA;</xsl:text>
> <xsl:variable name="length">
> <xsl:call-template name="format-time">
> <xsl:with-param name="x" select="songLengthInSeconds" />
> </xsl:call-template>
> </xsl:variable>
> Length (hh:mm:ss): <xsl:value-of select="$length"
> /><xsl:text>&#xA;</xsl:text>
> </xsl:template>
>
> <xsl:template name="format-time">
> <xslaram name="x" />
>
> <xsl:value-of select="format-number(floor(($x div 60) div 60),'00')"
> />:<xsl:value-of select="format-number(floor(($x div 60) mod
> 60),'00')" />:<xsl:value-of select="format-number($x mod 60,'00')" />
> </xsl:template>
>
> </xsl:stylesheet>
>
> Regards,
> Mukul
>
> "T-Narg" <(E-Mail Removed)> wrote in message

news:<(E-Mail Removed)>...
> > I would like to produce the following output based on my XML file:
> >
> > My Album (2005)
> > Elapsed Time (hh:mm:ss): 00:07:00
> >
> > Song 1: title1
> > Length (hh:mm:ss): 00:02:30
> >
> > Song 2: title2
> > Length (hh:mm:ss): 00:02:15
> >
> > Song 3: title3
> > Length (hh:mm:ss): 00:02:15
> >
> >
> > =====
> >
> > <album>
> > <general>
> > <title>My Album</title>
> > <year>2005</year>
> > </general>
> >
> > <content>
> > <song>
> > <songTitle>title1</songTitle>
> > <songLengthInSeconds>150</songLengthInSeconds>
> > </song>
> > <song>
> > <songTitle>title2</songTitle>
> > <songLengthInSeconds>135</songLengthInSeconds>
> > </song>
> > <song>
> > <songTitle>title3</songTitle>
> > <songLengthInSeconds>135</songLengthInSeconds>
> > </song>
> > </content>
> > </album>
> >
> > =====
> >
> > I need some help in designing the XSLT file. I'd like the
> > <songLengthInSeconds> to be formatted in hh:mm:ss format. Also, I want

to
> > display the elapsed time (in hh:mm:ss format) based on a total of
> > <songLengthInSeconds>. Could I perform this summation inside the XSLT?
> >
> > Thank you very much!



 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vista: "Calculating time remaining" takes long time on any fileaction. Even with all suggested KB patches installed Peter Computer Support 2 12-15-2007 12:57 AM
BT Total Broadband vs BT Business Total John Y UK VOIP 2 02-02-2007 09:27 AM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Calculating time of employee session from the log date/time stampusing perl Jayesh Kamdar Perl Misc 2 05-25-2005 12:08 AM
Help! I am having problems with calculating the total seconds from two date/times. Tim ASP General 3 08-05-2003 07:45 PM



Advertisments