[xsl] [XSLT 2.0] Determining the datatype of the value returned from a function?

Subject: [xsl] [XSLT 2.0] Determining the datatype of the value returned from a function?
From: "Roger L. Costello" <costello@xxxxxxxxx>
Date: Fri, 17 Jun 2005 11:54:27 -0400
Hi Folks,
 
I am trying to create an XSLT function which returns a value that may be one
of any number of different (atomic) datatypes, e.g., xs:integer, xs:double,
xs:decimal, xs:string, etc.  With the value that the function returns I
would like to determine its datatype.  For example, if the function returns
the integer 5 then: 
 
    "$value instance of xs:integer" 
 
should yield true (assume that $value is a variable that holds the value
returned from the function).
 
Below is my attempt at solving this problem.  It does not work.  The output
that I get is: text().  Do you have a suggestion on how to solve this
problem?  (I am using SAXON 8.4) /Roger
 
<xsl:function name="ex:Test">
    <xsl:param name="letter"/>
    <xsl:choose>
         <xsl:when test="$letter eq 'A'">
             <xsl:variable name="num" select="5" as="xsd:integer"/>
             <xsl:value-of select="$num"/>
         </xsl:when>
         <xsl:when test="$letter eq 'B'">
             <xsl:variable name="num" select="5.00" as="xsd:decimal"/>
             <xsl:value-of select="$num"/>
         </xsl:when>
         ...
         <xsl:otherwise>
             <xsl:variable name="message" select="'Error'" as="xsd:string"/>
             <xsl:value-of select="$message"/>
         </xsl:otherwise>
    </xsl:choose>
</xsl:function>
 
<xsl:template match="/">
    <xsl:variable name="test1" select="'A'"/>
    <xsl:variable name="result" select="ex:Test($test1)"/>
    <xsl:choose>
        <xsl:when test="data($result) instance of xsd:integer">
            <xsl:message>INTEGER</xsl:message>
        </xsl:when>
        <xsl:when test="data($result) instance of xsd:string">
            <xsl:message>STRING</xsl:message>
        </xsl:when>
        ...
        <xsl:otherwise>
             <xsl:message>OTHER</xsl:message>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Current Thread