Processing an Integer Buried in a String in 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 an开发者_开发问答d 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.
Here are the modifications I needed to make my original attempt work. I needed to remember my dollar signs for variables. I needed to use div
instead of /
. Most importantly, I needed the expression number
to cast the substring a number. This is what I've come up with. I welcome commentary that will improve my XSLT, though!
<xsl:variable name="time"><xsl:value-of select="time" /><xsl:variable>
<xsl:variable name="totalHours><xsl:value-of select="number(substring-before($time, ':'))" /></xsl:variable>
<xsl:variable name="days"><xsl:value-of select="floor($totalHours div 24)" /></xsl:variable>
<xsl:variable name="hours"><xsl:value-of select="$totalHours mod 24" /></xsl:variable>
<xsl:variable name="minutes"><xsl:value-of select="substring-after(time, ':')" /></xsl:variable>
精彩评论