Is there any official XSLT validator, like http://validator.w3.org/ for HTML & XHTML?
I know http://validator.w3.org/ can also be used to validate XML and XSL documents. But they are validated as XML structures.
In valid XHTML, for example, <span><div>...</div></span>
is invalid, as block elements should not appear inside in-line elements.
So, is there a convenient way, to directly validate the XSL document?
(i guess we may combine XML and XSL, output the XHTML, then validate it. But this is not directly and not convenient.)
Thanks!
For this you need a schema-aware XSLT processor that does static checking (for example Saxon-EE), and you need the stylesheet to (a) import the schema using xslt:import-schema, and (b) to invoke validation on the result elements using [xsl:]validation="strict".
XSLT is very widely used. As far as we can judge from metrics like the number of StackOverflow questions, it is in the top 30 programming languages, which probably makes it the top data-model-specific programming language after SQL. But XSLT isn't widely used client-side, that is, in the browser.
Try running Chrome with the --allow-file-access-from-files flag. I've not tested this myself, but if it works, your system will now also be vulnerable to scenarios of the kind mentioned above. Upload it to a host, and problem solved.
I assume that by validating XSLT stylesheets you mean checking whether the elements, attributes etcetera in an already well-formed XML document (which appears to be XSLT code) adhere to the XSLT W3C specification.
First of all, note that there is a difference between the well-formedness and validity of an XML document. It is well-formed if the contained elements are properly nested, if there is a single root element and so forth (this is defined by the XML specification). Also see: Is there a difference between 'valid xml' and 'well formed xml'? .
The validity of an XML document can only be verified together with an XML schema (or DTD, or RelaxNG...). A schema is a set of rules defining, for example, which elements and attributes are allowed in what sequence.
Now, to answer your question: There is no such service from W3C, however, there is a schema available, see e.g. http://www.w3.org/2007/schema-for-xslt20.xsd for a schema that incorporates all the structures of "standard" XSLT 2.0
.
You can validate your XSLT code against this schema. Still, it is more insightful to just run your code with an XSLT processor and look for the warnings and errors it produces.
Besides, be aware that the validity of XSLT code and the validity of the XHTML it outputs is not the same. Even if your XSLT is perfectly valid with respect to the XSLT specification, it does not mean that the resulting XHTML is reasonable.
In the general case you can't take an arbitrary XSLT stylesheet and prove statically that it will generate valid XHTML. You can however detect quite a few cases where it won't, provided that the stylesheet is written to take advantage of schema-awareness. For this you need a schema-aware XSLT processor that does static checking (for example Saxon-EE), and you need the stylesheet to (a) import the schema using xslt:import-schema, and (b) to invoke validation on the result elements using [xsl:]validation="strict". This will detect some validity errors in your stylesheet output statically (while compiling the stylesheet), and the remainder dynamically (while running it).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With