RE: [xsl] Validating XML against a Schema using XSLT

Subject: RE: [xsl] Validating XML against a Schema using XSLT
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 19 Jul 2007 11:06:26 +0100
Actually, whether you use a schema-aware XSLT 2.0 processor or a plain
vanilla XSLT 1.0 processor, validation of the source document is something
you invoke as a separate operation before the transformation starts. There
may be an API that makes it seamless, or you might do the two steps using
completely different products invoked independently, but logically they are
separate operations.

The difference if you use a schema-aware XSLT processor is that the XSLT
processing takes account of information gained during validation, for
example it knows which attributes are numbers or dates, and it knows where
in the document a <title> element might be found.

You can invoke validation from within XSLT if you have a schema-aware
processor: you can do <xsl:copy-of select="/" validation="strict"/>.
However, the normal assumption is that it's better to do the validation
on-the-fly while the source document is being parsed rather than creating a
validated copy of the original unvalidated input. In Saxon there are various
API and command-line switches to say "parse this document (or all documents)
with schema validation switched on".

Michael Kay
http://www.saxonica.com/



> -----Original Message-----
> From: Mark Anderson [mailto:mark.anderson@xxxxxxxxxxxxxxxxxxx] 
> Sent: 19 July 2007 10:48
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Validating XML against a Schema using XSLT
> 
> Hi All
> 
> I'm working on a new XSL project where I will receive and XML 
> from an external system, and transform it into a T-SQL script.
> 
> I have created a schema for the supplied XML and I want to 
> find a way to validate the XML against the schema before I 
> even start to transform it to T-SQL (this will dramatically 
> simplify my stored procedure, as I can skip most of the error 
> checking for required fields and correct data
> types)
> 
> Is there a way to do this with XSL? Right now I'm using 1.0 
> and MSXML, but if it required a SA XSLT processor, we could 
> spring for that.
> 
> Regards
> 
> 
> M a r k 

Current Thread