Hi all,
I'm trying to understand a simple concept with XSLT 3.0, when we use
'as' attribute with xsl:variable instruction. I'm using Saxon HE 12.2.
I'd like to understand these concepts with a non schema aware XSLT
transformation.
My XSLT stylesheet is as follows (this one doesn't use 'as' attribute on
xsl:variable instruction),
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="var1" select="'4'"/>
<xsl:template match="/">
<result>
<xsl:value-of select="$var1 + 3"/>
</result>
</xsl:template>
</xsl:stylesheet>
Running the above XSLT transformation, produces following error,
Error in xsl:value-of/@select on line 13 column 44 of test1.xsl:
XPTY0004 Arithmetic operator is not defined for arguments of types
(xs:string, xs:integer)
I'm fine with above produced XSLT transformation error.
Now I change the above cited XSLT stylesheet to following (I'm introducing
an 'as' attribute to xsl:variable instruction),
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="var1" select="'4'" as="xs:integer"/>
<xsl:template match="/">
<result>
<xsl:value-of select="$var1 + 3"/>
</result>
</xsl:template>
</xsl:stylesheet>
When I run this new XSLT transformation, I get following error,
Error at xsl:variable on line 9 column 61 of test1.xsl:
XTTE0570 The required item type of the value of variable $var1 is
xs:integer. The
supplied value is of type xs:string
This error confuses me. I think that, the new XSLT transformation that I've
tried should have succeeded.
The XSLT 3.0 spec says following, about xsl:variable instruction,
[1]
<quote>
An optional "as" attribute specifies the required type of the variable. The
value of the "as" attribute is a 'SequenceType'.
If the "as" attribute is specified, then the supplied value of the variable
is converted to the required type. [ERR XTTE0570] It is a type error if the
supplied value of a variable cannot be converted to the required type. If
the "as" attribute is omitted, the supplied value of the variable is used
directly, and no conversion takes place.
</quote>
I think the string value '4' of variable var1, could be easily converted to
an xs:integer value (since, the string value '4', contains all numeric
characters).
By reading the above quoted text [1] from XSLT 3.0 spec, I think that, the
second XSLT transformation that I've cited should have been able to do an
addition $var1 + 3 without issues.
Any thoughts please.
--
Regards,
Mukul Gandhi
|