Kapil
Kapil

Reputation: 832

Convert String to Integer in XSLT 1.0

I want to convert a string value in xslt to an integer value. I am using xslt 1.0, so i can't use those functions supported in xslt 2.0. Please help.

Upvotes: 41

Views: 161792

Answers (2)

jeffreypriebe
jeffreypriebe

Reputation: 2287

Adding to jelovirt's answer, you can use number() to convert the value to a number, then round(), floor(), or ceiling() to get a whole integer.

Example

<xsl:variable name="MyValAsText" select="'5.14'"/>
<xsl:value-of select="number($MyValAsText) * 2"/> <!-- This outputs 10.28 -->
<xsl:value-of select="floor($MyValAsText)"/> <!-- outputs 5 -->
<xsl:value-of select="ceiling($MyValAsText)"/> <!-- outputs 6 -->
<xsl:value-of select="round($MyValAsText)"/> <!-- outputs 5 -->

Upvotes: 72

jelovirt
jelovirt

Reputation: 5892

XSLT 1.0 does not have an integer data type, only double. You can use number() to convert a string to a number.

Upvotes: 35

Related Questions