Processing an Integer Buried in a String in XSL
Posted
by justkt
on Stack Overflow
See other posts from Stack Overflow
or by justkt
Published on 2010-05-03T17:02:20Z
Indexed on
2010/05/03
17:28 UTC
Read the original article
Hit count: 535
xsl
I have a stored time value which is of the format H:mm:ss
. The hours may be any value from 0 up through several days. This data is sent in an XML tag and processed by XSL to be displayed. The display that I want is of the format:
D days, HH:mm:ss (hours/minutes)
Where the last tag shows hours if HH is greater than 0, minutes if it is 0.
Given the original HH, which may be more than 24, I know I need the floor of HH / 24 to get the days value. Then the original HH % 24 gives me the leftover hours.
I have also handled the minutes and hours question using xsl:when
and xsl:if
.
It's getting days and hours from the hours value that has me stumped.
EDIT So far, I'm looking at doing the following:
Variable declaration
<xsl:variable name="time"><xsl:value-of select="time" /><xsl:variable>
<xsl:variable name="days"><xsl:value-of select="floor(substring-before(time, ':') / 24)" /></xsl:variable>
<xsl:variable name="hours"><xsl:value-of select="substring-before(time, ':') mod 24" /></xsl:variable>
<xsl:variable name="minutes"><xsl:value-of select="substring-after(time, ':')" /></xsl:variable>
Use
<xsl:if test="$days > 0">
<xsl:value-of select="$days" /> days
</xsl:if>
<xsl:value-of select="$hours" />:<xsl:value-of select="$minutes" />
<xsl:choose>
<xsl:when test="$hours > 0">
hour<xsl:if test="$hours > 1">s</xsl:if>
</xsl:when>
<xsl:otherwise>
minute<xsl:if test="$minute != '01:00'">s</xsl:if>
</xsl:otherwise>
</xsl:choose>
And for clarification, a sample time would be <time>26:15:00</time>
for 1 day 2:15 hours.
© Stack Overflow or respective owner