可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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!
回答1:
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.
回答2:
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).
回答3:
There are many XML editing tools (and probably a few HTML ones as well) that know the structure of (X)HTML enough to highlight errors. Visual Studio for example can validate ANY XML document using the schema, including XHTML or XSL- Whether or not a div is allowed inside a span is defined in the XHTML schema.