41

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.

Kapil
  • 832
  • 2
  • 14
  • 29
  • 2
    Could you provide a sample input XML document, a sample XSLT and possibly describe the desired output? – Dirk Vollmar Aug 12 '09 at 12:33
  • you can take any sample file and convert the string to integer. I guess no need for the samples – Kapil Aug 12 '09 at 12:36
  • But how do you want your output to look like? And what do you want to do with that number? It is very hard to help without knowing what you are trying to achieve. – Dirk Vollmar Aug 12 '09 at 12:37
  • oh actually i want to validate that converted number against a schema. I am having two schemas one is input and other output.The data is getting validated by input schema because that feild was string in that but in the output schema the same feild is integer.I cannot change schemas and i have to do this in xslt only – Kapil Aug 12 '09 at 12:40
  • 2
    So could you show a short sample? – Dirk Vollmar Aug 12 '09 at 12:42
  • here u go: Dew 1234 The second child node is actually a string from source and i wanted to convert that to integer. I tried number('1234') but it's not working.Please help – Kapil Aug 12 '09 at 12:47
  • 3
    "not working" is perhaps the best way to *avoid* getting a valid answer. If number() isn't doing what you want, then it seems that you're trying to do something that doesn't really require a number. So, as *divo* asked, provide sample input, sample output, and the XSLT that you're using. And do it in your question, formatted as code, not in a comment. – kdgregory Aug 12 '09 at 12:53
  • How can we format a string if it is having $ and comma signs like $ 2,000.00 i want to sum multiple values but these are dollar and comma separated. first i need to convert them in number and then want to sum them up. is there any solution ? – M.I.A Aug 16 '18 at 11:53

2 Answers2

70

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 -->
jeffreypriebe
  • 2,267
  • 25
  • 30
  • 1
    In XPath 1.0, if a function (like `floor()`, `ceiling()` or `round()`) expects a number data type argument, then it's going to implicitly cast its argument with `number()` function. –  Jan 25 '11 at 21:56
  • So my "number($MyValAsText)" is superfluous? Well, let's save those bits then! Thanks for the tip Alejandro. I've gotten bit by not casting enough times in XSLT that I generally cast too often (case in point). – jeffreypriebe Jan 26 '11 at 23:33
35

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

jelovirt
  • 5,844
  • 8
  • 38
  • 49