Re: [xsl] Schema validation on a function parameter

Subject: Re: [xsl] Schema validation on a function parameter
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 12 Feb 2018 17:18:21 -0000
You can simplify it a little to

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

If you do this a lot you could also try

<xsl:function name="my:validate" as="element(*, xs:anyType)">
  <xsl:param name="e" as="element()"/>
   <xsl:variable name="e" as="schema-element(*, xs:anyType)">
       <xsl:copy-of select="$e" validation="strict"/>
   </xsl:variable>
</xsl:function>

<xsl:function name="my:function">
   <xsl:param name="e" as="element()"/>
   ... do something here with my:validate($e)
 </xsl:function>

Michael Kay
Saxonica


> On 12 Feb 2018, at 12:07, Matthieu RICAUD-DUSSARGET
m.ricaud-dussarget@xxxxxxxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> 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