Search code examples
xslt-2.0

Explicitly Typed variables in XSL


I'm using XSL's convenience functions for comparisons, gt, lt, ge, le, eq.

I understand these functions won't promote a string to a numerical value when performing comparisons, however I need that cast to be made, and I don't want to clutter my code with lines like

<xsl:when test="xs:integer($variable) lt 250" >

I'd rather make that cast like this (hypothetical of course)

<xsl:variable name="variable" type="xs:integer">

So, is there a means of explicitly casting variable as an numerical type when it is declared/created ?


Solution

  • <xsl:when test="xs:integer($variable) lt 250" >
    

    I'd rather make that cast like this (hypothetical of course)

    <xsl:variable name="variable" type="xs:integer">
    

    Use the as attribute -- its purpose is exactly to specify the type of a variable, parameter, template or a function:

    <xsl:variable name="variable" as="xs:integer" 
                  select="some-integer-type-expression">