How to get the value type

Teach me Please.

Is there a way to get the type of the variable you specify? For example, I'm thinking about the use of getting a type and outputting it to a message, or comparing types to make a condition as follows.

Or is it possible to do it?

<!-- example -->
<xsl:message select="[get_type_function]([any_variable_as_string])" />

<!-- message -->
xs:string

Best Regards.

1 answer

  • answered 2018-07-12 06:48 Martin Honnen

    What do you expect the type of a variable to be, if a declare <xsl:variable name="var1" as="item()*" select="1"/>, is the type you want then the declared item()* or is it xs:integer or xs:integer*? In the context of XSLT 1 and EXSLT there is http://exslt.org/exsl/functions/object-type/index.html to get the rudimentary types 'string', 'number', 'boolean', 'node-set', 'RTF' or 'external', but XSLT/XPath 1 have a rather simple type system and don't declare types anyway, in XSLT/XPath 2 or 3 the type system is much more complex.

    In the context of Saxon 9 EE or PE there is http://saxonica.com/html/documentation/functions/saxon/type.html so for the above sample

        <xsl:variable name="var1" as="item()*" select="1"/>
        <xsl:message select="saxon:type($var1)('name')" xmlns:saxon="http://saxon.sf.net/"/>
        <xsl:variable name="var2" as="item()*" select="'foo'"/>
        <xsl:message select="saxon:type($var2)('name')" xmlns:saxon="http://saxon.sf.net/"/>
    

    you get

    integer
    string
    

    but I am not sure how you would get a fitting type for a heterogeneous sequence as

        <xsl:variable name="var3" as="item()*" select="1, 'foo'"/>
        <xsl:message select="saxon:type($var3)('name')" xmlns:saxon="http://saxon.sf.net/"/>
    

    also simply gives integer.