I’m trying to understand how schema-element() works when used as a SequenceType with literal result elements. This is my stylesheet
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:import-schema>
<xs:schema>
<xs:element name="foo">
<xs:complexType>
<xs:attribute name="bar"/>
</xs:complexType>
</xs:element>
</xs:schema>
</xsl:import-schema>
<xsl:variable name="test" as="schema-element(foo)">
<foo bar="baz"/>
</xsl:variable>
<xsl:template match="/">
<xsl:sequence select="$test"/>
</xsl:template>
</xsl:stylesheet>
Saxon-EE 9.4.0.4 (in Oxygen 14.1) is giving me the following compilation error Required item type of value of variable $test is schema-element(foo); supplied value has item type element(foo, {http://www.w3.org/2001/XMLSchema}untyped)
According to XSLT 2.0 and XPath 2.0 Programmer’s Reference
When the construct «schema-element(N)» … is used in a SequenceType,
then N must be the name of a global element … declaration in an
imported schema.
elsewhere in the same source
«as=”schema-element(EVENT)”» indicates that [a parameter] must be an element node validated as being
either an EVENT or an element in the substitution group of EVENT.
It seems like <foo bar="baz"/> should validate as a foo element by the imported schema. @xsl:type seems like it would help, but only if I had a named complex type in my imported schema. What am I missing or doing wrong?
Have you tried to use
<foo xsl:validation="strict" bar="baz"/>? The error message suggests the result element is created without type annotations so perhaps the validation needs to be requested explicitly, theasattribute might not suffice. But I am not too familiar with schema aware XSLT 2.0 so it is a suggestion for you to try.