xml 官方 XSLT 验证器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20605328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:57:32  来源:igfitidea点击:

Official XSLT validator?

xmlxsltxhtmlvalidation

提问by midnite

Is there any official XSLT validator, like http://validator.w3.org/for HTML & XHTML?

是否有任何官方的 XSLT 验证器,例如HTML 和 XHTML 的http://validator.w3.org/

I know http://validator.w3.org/can also be used to validate XML and XSL documents. But they are validated as XML structures.

我知道http://validator.w3.org/也可用于验证 XML 和 XSL 文档。但是它们被验证为 XML 结构。

In valid XHTML, for example, <span><div>...</div></span>is invalid, as block elements should not appear inside in-line elements.

例如,在有效的 XHTML 中<span><div>...</div></span>是无效的,因为块元素不应出现在行内元素中。

So, is there a convenient way, to directly validate the XSL document?

那么,有没有方便的方法,直接验证XSL文档呢?

(i guess we may combine XML and XSL, output the XHTML, then validate it. But this is not directly and not convenient.)

(我想我们可以结合 XML 和 XSL,输出 XHTML,然后验证它。但这不是直接的,也不方便。)

Thanks!

谢谢!

回答by Mathias Müller

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.

我假设通过验证 XSLT 样式表,您的意思是检查格式良好的 XML 文档(似乎是 XSLT 代码)中的元素、属性等是否符合 XSLT W3C 规范。

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'?.

首先,请注意 XML 文档的格式良好和有效性之间存在差异。如果包含的元素正确嵌套,如果有单个根元素等等(这是由 XML 规范定义的),则它是格式良好的。另请参阅:“有效的 xml”和“格式良好的 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.

XML 文档的有效性只能与 XML 模式(或 DTD,或 RelaxNG...)一起进行验证。模式是一组规则,例如定义允许以什么顺序使用哪些元素和属性。

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.xsdfor a schema that incorporates all the structures of "standard" XSLT 2.0.

现在,回答您的问题:W3C 没有这样的服务,但是,有一个可用的模式,请参阅例如http://www.w3.org/2007/schema-for-xslt20.xsd以获取包含所有“标准”的结构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.

您可以根据此模式验证 XSLT 代码。尽管如此,使用 XSLT 处理器运行您的代码并查找它产生的警告和错误会更有见地。



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.

此外,请注意 XSLT 代码的有效性和它输出的 XHTML 的有效性是不一样的。即使您的 XSLT 相对于 XSLT 规范完全有效,也不意味着生成的 XHTML 是合理的。

回答by Michael Kay

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).

在一般情况下,您不能采用任意 XSLT 样式表并静态证明它将生成有效的 XHTML。但是,您可以检测到很多情况下它不会,只要编写样式表以利用模式感知。为此,您需要一个执行静态检查的模式感知 XSLT 处理器(例如 Saxon-EE),并且需要样式表 (a) 使用 xslt:import-schema 导入模式,以及 (b) 调用验证结果元素使用 [xsl:]validation="strict"。这将静态检测样式表输出中的一些有效性错误(在编译样式表时),并动态检测其余部分(在运行时)。

回答by Flynn1179

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.

有许多 XML 编辑工具(可能还有一些 HTML 工具)知道 (X)HTML 的结构足以突出显示错误。例如,Visual Studio 可以使用架构验证任何 XML 文档,包括 XHTML 或 XSL-在 XHTML 架构中定义了跨度内是否允许 div。