Xslt 1.0 how to check the number

How can I check if the value of an element is a number / valid for a number?

I need to make a copy if it is a number and replace it with 99 if it is a string

+4
source share
3 answers

string (number ($ x)) = 'NaN' checks if $ x is valid for a number.

+2
source

In addition to the correct answer @ Michael Kay:

number($x) = number($x) 

this expression true() accurate when $x can be used for a number

Here we use the fact that:

  • If $x not a number, then number($x) by the definition of NaN

  • NaN not equal to any value, even NaN

Now your last question :

I need to do to copy the value if it is a number and replace it with 99 if it is a string

Using

  $x * (number($x) = number($x)) + 99 * not(number($x) = number($x)) 

The explanation . A boolean value is converted to a number ( true() โ†’ 1, false() โ†’ 0) with a partial expression. In the above expression, one of the + arguments will be 0 , and one will be 1 depending on whether $x / is a number.

+1
source

All Articles