xmlxslttimecodes

Converting number of drop frames to seconds in XSLT


I am currently working with a total number of frames from an XML. What I am attempting to do is convert this in an amount of time. I know that my fps is 30. I have successfully calculated the total number of frames I have and converted this into seconds. What I am attempting to do now is change this total seconds in hh:mm:ss format.

Here is what I have in my code:

<!-- call in template time-calc and then divid by 30fps to calulate total seconds of time-->
<xsl:variable name="TotalDurationShow"><xsl:call-template name="time-calculation"></xsl:call-template></xsl:variable>
<xsl:variable name="TotalSeconds" select="$TotalDurationShow div 30"/> 


<!-- Time-in-seconds to time in hh:mm:ss.000 form-->
<xsl:template name="secondsToTime">
    <xsl:param name="seconds" />
    <xsl:variable name="partHours"   select="floor($seconds div 60 div 60)" />
    <xsl:variable name="partMinutes" select="floor(($seconds - ($partHours * 60 * 60)) div 60)" />
    <xsl:variable name="partSeconds" select="$seconds - ($partHours * 60 * 60) - ($partMinutes * 60)" />
    <xsl:value-of select="concat(substring('0', 1, 2 - string-length(string( $partHours ))), $partHours)" /><xsl:text>:</xsl:text>
    <xsl:value-of select="concat(substring('0', 1, 2 - string-length(string( $partMinutes ))), $partMinutes)" /><xsl:text>:</xsl:text>
    <xsl:value-of select="concat(substring('0', 1, 2 - string-length(string( $partSeconds ))), $partSeconds)" />
</xsl:template>


<!-- test to make sure this is a number value in for total seconds
    call in template secondstotime on totalseconds variable to convert to time code formate hh:mm:ss-->
<xsl:if test="$TotalSeconds != 0 and string(number($TotalSeconds)) != 'NaN'">
    <xsl:variable name="TimeFrame"><xsl:call-template name="secondsToTime"><xsl:with-param name="seconds" select="$TotalSeconds" /></xsl:call-template></xsl:variable>
</xsl:if>

I am getting an error message that xsl:if cannot be a child of the spread sheet. When I remove the test for the if, I get an error message saying the param seconds is not defined. Am I doing something incorrectly when calling in the template secondstotime on my variable totalseconds?


Solution

  • how to convert the total number of seconds which I have calculated (This example the value is 2670) and put that into an output that displays hh:mm:ss

    Given:

    <xsl:variable name="TotalSeconds" select="2670"/>
    

    you can use:

    <xsl:variable name="h" select="floor($TotalSeconds div 3600)"/>
    <xsl:variable name="m" select="floor($TotalSeconds div 60) mod 60"/>
    <xsl:variable name="s" select="$TotalSeconds mod 60"/>
    
    <xsl:value-of select="concat(format-number($h, '00'), format-number($m, ':00'), format-number($s, ':00'))" />
    

    to return:

    00:44:30