0

Given below is the xml.

I want to display time difference like: "2h 52m".

START TIME - 09:02

STOP TIME - 11:45

TOTAL -2h 52m

<Surgery>
   <SURGERY_START_TIME>9:02</SURGERY_START_TIME>
   <SURGERY_STOP_TIME>11:45</SURGERY_STOP_TIME>
</Surgery>

I tried the below code but that doesn't work,

<xsl:variable name="surgStartTime">
        <xsl:value-of select="SURGERY_START_TIME/."/>
</xsl:variable>
<xsl:variable name="surgStopTime">
        <xsl:value-of select="SURGERY_STOP_TIME/."/>
</xsl:variable>
<xsl:value-of select="$surgStartTime -$surgStopTime)/>
Azhar Khan
  • 3,829
  • 11
  • 26
  • 32
vishnu
  • 195
  • 1
  • 3
  • 19
  • You can do computations on proper `xs:time`s e.g. `xs:time('09:02:00') - xs:time('11:45:00')` but the result is not 2h52m I think but 2h43m. So before you do time arithmetics you will need to fix the input (e.g. with regular expressions) to get the right format for `xs:time`. – Martin Honnen Nov 22 '22 at 10:24
  • Which version of XSLT does your processor support? – michael.hor257k Nov 22 '22 at 10:29
  • Hi thank you. I think – vishnu Nov 22 '22 at 10:31
  • Can you please show an example, how to apply regular expression on the input 9:02 and 11:45? – vishnu Nov 22 '22 at 10:36
  • That does not answer my question. See here how to test your processor: https://stackoverflow.com/a/25245033/3016153. If it only supports XSLT 1.0 then you cannot use time arithmetic or regex. – michael.hor257k Nov 22 '22 at 10:42
  • ok. I just checked...my processor supports xslt 1.0, but can I use xs:time if not regular expressions? – vishnu Nov 22 '22 at 10:53

1 Answers1

0

There is no support for date/time arithmetic in XSLT 1.0; you need to do the calculation yourself - e.g:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="Surgery">
    <!-- calculate the duration in minutes -->
    <xsl:variable name="duration" select="60 * substring-before(SURGERY_STOP_TIME, ':') + substring-after(SURGERY_STOP_TIME, ':') - 60 * substring-before(SURGERY_START_TIME, ':') - substring-after(SURGERY_START_TIME, ':')" />
    <xsl:copy>
        <xsl:copy-of select="*"/>
        <DURATION>
            <!-- output whole hours -->
            <xsl:value-of select="floor($duration div 60)"/>
            <xsl:text>h </xsl:text>
            <!-- output remaining minutes -->
            <xsl:value-of select="$duration mod 60"/>
            <xsl:text>m</xsl:text>
        </DURATION>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Applied to your input example, the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<Surgery>
   <SURGERY_START_TIME>9:02</SURGERY_START_TIME>
   <SURGERY_STOP_TIME>11:45</SURGERY_STOP_TIME>
   <DURATION>2h 43m</DURATION>
</Surgery>

and not 2h 52m as stated in your question.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51