Search code examples
xmlxslt-2.0

Required item type of value of variable $duration is xs:string supplied value has item type xs:dayTimeDuration


I have got two different timestamps in milliseconds. For these two timestamps I want to know a duration between them. The duration is correct calculated, but I want format this duration and then the Eclipse give me this error:

Required item type of value of variable $duration is xs:string supplied value has item type xs:dayTimeDuration

<xsl:variable name="start" select='xs:dateTime("1970-01-01T00:00:00") + 1512037112064 * xs:dayTimeDuration("PT0.001S")'/>
<xsl:variable name="end" select='xs:dateTime("1970-01-01T00:00:00") + 1512059856670 * xs:dayTimeDuration("PT0.001S")'/>

<xsl:value-of select="xs:dateTime($end)-xs:dateTime($start)" />

Output without any formation

PT6H19M4.606S

Wanted formatted output

6:19:4.606


Solution

  • Firstly, if you have two timestamps $endTS and $startTS in milliseconds the easiest way to get the duration between them is

    xs:dayTimeDuration('PT0.001S') * ($endTS - $startTS)
    

    You don't need to add both to a dummy start time and then subtract two dummy dateTime values.

    Secondly, if you want to format a duration in H:M:S format, then if you know the duration is less than 24 hours, you can use

    format-time(xs:time('00:00:00') + $duration, '[H1]:[m1]:[s1].[f000]')
    

    If you don't know it's less than 24 hours you can use

    concat(days-from-duration($d)*24 + hours-from-duration($d), ':',
      minutes-from-duration($d), ':', seconds-from-duration($d))
    

    I don't know what caused the type error; the code you have shown does not declare or use a variable called $duration.