xslt check if a variable is a decimal with digits after decimal seperator

Question

I want to show decimal inputs like 100.1, 100.0 in a different format from integer inputs like 100 I have used the following check for identifying decimals(The variable input is of numeric type)

 <xsl:when test="not(floor($input) = $input)"> 

It works for cases like 100.1 but it does not work for cases like 100.00, this would return false saying that this is not a decimal. I have then tried to use

<xsl:when test="$input castable as xs:decimal">

This is would not work for integers like 100 as the check would return true. I tried

<xsl:when test="$input instance of xs:decimal">

for which I am getting false for values like 100.50, not sure why. Is there any other way I can solve this?


Show source
| integer   | xslt   | decimal   | xslt-2.0   2016-11-17 09:11 2 Answers

Answers ( 2 )

  1. 2016-11-17 09:11

    If you want to distinguish between 100 and 100.0, you cannot be in the numeric domain. Try perhaps:

    <xsl:when test="contains($input, '.')">
    

    or use regex.

  2. 2016-11-17 11:11

    You really need to show how you have declared the type of the variable $input (or in the absence of a type declaration, how you have initialized its value). It makes a big difference.

    Note that 100.5 as an XPath literal produces the xs:decimal value 100.5, but 100.5 held as the value of an untyped attribute node will be converted to an xs:double value approximately equal to 100.5.

    In principle you can identify decimals that are not integers using

    $x instance of xs:decimal and not($x instance of xs:integer)
    

    but that assumes that you created the value as an xs:decimal or xs:integer in the first place; if you do xs:decimal("100") then you will probably[*] get an xs:decimal that is not an xs:integer.

    [*] I say probably, because the spec requires it to be an xs:decimal, but it arguably permits it to be a subtype of xs:decimal, e.g. it could even return an instance of xs:unsignedByte here if it chose, or for that matter an instance of saxon:integer-in-range-1-to-100, so long as that is defined as a subtype of xs:decimal.]

◀ Go back