XSLT conditional select variable

I want the variable to have an element value if the value is numeric, but if it is not, I want the variable to have a value of 0 .

In other words, is there a simple equivalent of the following in XSLT?

 var foobar = is_numeric(element value) ? element value : 0 

Or how do you write it?

 <xsl:variable name="foobar" select=" ? " /> 
+6
source share
3 answers

In XPath 2.0, yes, you can use " castable as "

 <xsl:variable name="foobar" as="xs:double" select="if (x castable as xs:double) then x else 0" /> 
+7
source

XPath 1.0:

 <xsl:variable name="foobar"> <xsl:choose> <xsl:when test="number($value) = number($value)"> <xsl:value-of select="$value"/> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> 

Link to this smart number($value) = number($value) numerical test: Dimitra Novachev’s answer to "Question Xpath, if the number" .

+8
source

You can use:

 <xsl:variable name="x" select="(ELEMENT[. castable as xs:double], 0)[1]"/> 

or

 <xsl:variable name="x" select="sum(ELEMENT[. castable as xs:double])"/> 
+6
source

Source: https://habr.com/ru/post/955108/


All Articles