[xsl] Schema validation on a function parameter

Subject: [xsl] Schema validation on a function parameter
From: "Matthieu RICAUD-DUSSARGET m.ricaud-dussarget@xxxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 12 Feb 2018 12:07:22 -0000
Hi !

I have a function which takes an element as parameter.
I want to validate this element against an xsd schema type definition.

I'm using XSLT 3.0 with a schema aware processor (SaxonEE 9.7.0.15)

At first, I thought I only have to :
<xsl:import-schema schema-location="my-schema.xsd"/>
And then type the parameter:
<xsl:param name="e" as=" schema-element(my:element)"/>
But it does not validate (when the xml in not valid I don't get any errors or
warnings)

Then after reading the XSLT 3.0 spec, I realized validation is processed only
on source or result-tree:
"The imported type definitions can be used for temporary nodes or for nodes on
a result tree just as much as for nodes in source documents"

So I found this solution :

<xsl:import-schema schema-location="my-schema.xsd"/>

<xsl:function name="my:function">
    <xsl:param name="e" as="element()"/>
    <xsl:variable name="e" as="document-node(schema-element(my:element))">
      <xsl:document validation="strict">
        <xsl:sequence select="$e"/>
      </xsl:document>
    </xsl:variable>
    ... do something here
  </xsl:function>

This works pretty well, I actualy get a fatal error with its description when
$e is not valid, which is exactly what I was looking for.

Any advices, comment on this, is it a good way to do ? or there is something
more obvious which I missed (preventing creating this document-node for
example?)

Thanks in advance,
Cheers
Matthieu

Current Thread