xsl-list
[Top] [All Lists]

Re: [xsl] Schema validation on a function parameter

2018-02-12 11:52:07
Thank you Michael, 

With the copy and the function this will be quite transparent to the 
developers, meaning it's quite easy to validate params of a function.

First time I need this actualy, but I'll consider moving this to a common 
function in our lib.

Regards,
Matthieu


-----Message d'origine-----
De : Michael Kay mike(_at_)saxonica(_dot_)com 
[mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com] 
Envoyé : lundi 12 février 2018 18:19
À : xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Objet : Re: [xsl] Schema validation on a function parameter

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(_dot_)ricaud-dussarget(_at_)lefebvre-sarrut(_dot_)eu 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>