Hirantha
Hirantha

Reputation: 337

XSLT error when doing mathematical operations

In a mule flow, I want to convert the time duration in format "hh:mm" in a xml file to seconds using XSLT.

For conversion I used the following,

<xsl:template match="EventDuration">
    <duration>
        <xsl:value-of select="60*substring-after(text(), ':') + 3600*substring-before(text(), ':')" />
    </duration>
</xsl:template>

But It gives the following error....

XPTY0004: Arithmetic operator is not defined for arguments of types (xs:integer, xs:string)

What is the problem of this approach?

Upvotes: 2

Views: 3017

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

You seem to be using XSLT 2.0, and in 2.0 you need to convert strings to numbers explicitly before doing arithmetic.

Your other option is to use date/time/duration arithmetic:

(xs:time(concat(text(), ":00") - xs:time("00:00:00")) div xs:dayTimeDuration("PT1S") 

Upvotes: 1

kjhughes
kjhughes

Reputation: 111491

Use number() to convert the result of substring-before() and substring-after() from a string to a number.

Upvotes: 2

Related Questions