15

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!

midnite
  • 5,157
  • 7
  • 38
  • 52
  • I don't see how this could work, except perhaps in the simplest of cases. You cannot validate a future product that depends on input as yet unknown. Consider a case where a stylesheet defines an element with a calculated name. How can the validator know what that name will eventually be? It could be "span" and it could be "div". – michael.hor257k Dec 16 '13 at 11:12
  • well... yes... thanks for comment. This inspires me, and bring up another question: with the same XML and XSL, will they, in some case, generate different XHTMLs? – midnite Dec 16 '13 at 17:35
  • And if we are only validating the XSL, will some weird (but valid) XML do some tricks (like injection) towards the resulting XHTML? – midnite Dec 16 '13 at 17:37
  • Your last two questions are not clear (to me, at any rate). Who are "they"? Same XML and XSLT should produce the same result - with minor (inessential) differences among the various processors. – michael.hor257k Dec 16 '13 at 17:47
  • Yes, "they" meant "XML and XSL". So, if the same pair of XML and XSL will produce the same result, why cant we input a pair of XML and XSL, and perform the validation against the XHTML rules? (it is not a "future unknown product".) – midnite Dec 17 '13 at 01:43
  • Why sure, you *can* do that. Except you said that "this is not directly and not convenient". I thought your question was about validating the XSL output against any possible (or even just reasonable) XML input. – michael.hor257k Dec 17 '13 at 06:48
  • The case is, i will supply the XML, and my users supply the XSL (passed through HTML Purifier). I prefer to combine XML and XSL at clients' browsers. So i would like to validate if the user supplied XSL will produce valid XHTML. As the XML is from myself, i can assume it is valid and without injections, and also of expected formats. One solution is, i use PHP to do the transform, and validate at W3C. But if i can validate the XSL document directly, the server can do less. – midnite Dec 17 '13 at 08:10

3 Answers3

13

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.

Community
  • 1
  • 1
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
  • Yes! Thanks very much for your nice answer, and providing the schema. And what you mentioned in the last paragraph is exactly what i want - to ensure the resulting XHTML is valid - as XHTML is the thing that the users really perceive. – midnite Dec 16 '13 at 17:16
  • You're welcome. Please consider accepting the answer that was most helpful to you (not necessarily mine). – Mathias Müller Dec 17 '13 at 08:18
  • Please continue your last paragraph, as that is what i originally want to do. And also, please refer to the details that i mentioned in [the comment of my question](http://stackoverflow.com/questions/20605328/official-xslt-validator/20608732?noredirect=1#comment30875736_20605328) above, for more details. I really hope you can solve this problem with me. Many thanks! – midnite Dec 17 '13 at 08:34
  • Now that I finally understood what you were asking: I do not know of a (web) service that performs stylesheet transformation _and_ XHTML validation. – Mathias Müller Dec 17 '13 at 08:42
  • Haha, sorry i didn't make it clear in the first time. So you think that we cannot **directly validate XSL again XHTML rules** (as XSL contains many XHTML tags indeed)? Even if someone provide such a service, it is still combining XML and XSL silently behind the scene? – midnite Dec 17 '13 at 09:19
  • 1
    Yes, exactly. XSLT code cannot be validated against the XHTML specification. Also, the outcome cannot be guessed reliably whithout actually transforming it. – Mathias Müller Dec 17 '13 at 09:23
  • Thanks very much! It seems that, if I want to validate the XSL document, I must do the transform by myself (my server). – midnite Dec 17 '13 at 09:34
  • However, XSD is a document specify the rules of a XSL document. Is it possible to put also the XHTML rules into it? – midnite Dec 17 '13 at 09:36
  • 1
    XSD does not mean _THE_ schema for XSLT, but is a generic name for schemas. You cannot include rules for XHTML in the XSLT schema, but of course, XHTML has its own schema (e.g. http://www.w3.org/TR/xhtml11/xhtml11_schema.html for `XHTML 1.1`). [Please avoid extending this discussion any further.] – Mathias Müller Dec 17 '13 at 10:08
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).

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

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.

Flynn1179
  • 11,925
  • 6
  • 38
  • 74
  • Thanks for reply. Do you have any other IDE suggestions? Especially the free ones, like the Eclipse PDT (do you think it can)? – midnite Dec 16 '13 at 10:26
  • Not sure about eclipse, but check out this question, I think it's similar to what you're asking: http://stackoverflow.com/questions/124865/xml-schema-xsd-validation-tool – Flynn1179 Dec 16 '13 at 10:37