[quoted text, click to view] Uncle Ben wrote:
> 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?
Yes, certainly, you simply need basic math to split up the seconds value
into hours, minutes, and seconds and then you need to format the output.
Here is an example stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" />
<xsl:template match="album">
<xsl:value-of select="general/title" />
<xsl:text> (</xsl:text>
<xsl:value-of select="general/year" />
<xsl:text>) </xsl:text>
<xsl:text>Elapsed Time : </xsl:text>
<xsl:call-template name="format-seconds">
<xsl:with-param name="seconds"
select="sum(content/song/songLengthInSeconds)" />
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:apply-templates select="content/song" />
</xsl:template>
<xsl:template match="song">
<xsl:text>Song </xsl:text>
<xsl:value-of select="position()" />
<xsl:text>: </xsl:text>
<xsl:value-of select="songTitle" />
<xsl:text> </xsl:text>
<xsl:text>Length : </xsl:text>
<xsl:call-template name="format-seconds">
<xsl:with-param name="seconds" select="songLengthInSeconds" />
</xsl:call-template>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template name="format-seconds">
<xsl:param name="seconds" />
<xsl:variable name="seconds-per-hour" select="3600" />
<xsl:variable name="seconds-per-minute" select="60" />
<xsl:variable name="hours" select="floor($seconds div
$seconds-per-hour)" />
<xsl:variable name="minutes" select="floor(($seconds - $hours *
$seconds-per-hour) div $seconds-per-minute)" />
<xsl:variable name="remaining-seconds" select="$seconds - $hours *
$seconds-per-hour - $minutes * $seconds-per-minute" />
<xsl:value-of select="format-number($hours, '00')" />
<xsl:text>:</xsl:text>
<xsl:value-of select="format-number($minutes, '00')" />
<xsl:text>:</xsl:text>
<xsl:value-of select="format-number($remaining-seconds, '00')" />
</xsl:template>
</xsl:stylesheet>
Output is text of the form:
My Album (2005)
Elapsed Time : 00:07:00
Song 1: title1
Length : 00:02:30
Song 2: title2
Length : 00:02:15
Song 3: title3
Length : 00:02:15
--
Martin Honnen