XPath test if node value is number

Harold Sota picture Harold Sota · Oct 4, 2010 · Viewed 99.7k times · Source

How can I check if a node value is a number using XPath?

Any ideas?

Answer

Oded picture Oded · Oct 4, 2010

Test the value against NaN:

<xsl:if test="string(number(myNode)) != 'NaN'">
    <!-- myNode is a number -->
</xsl:if>

This is a shorter version (thanks @Alejandro):

<xsl:if test="number(myNode) = myNode">
    <!-- myNode is a number -->
</xsl:if>