简体   繁体   English

如何针对XML模式验证XSLT模板的逻辑?

[英]How can I validate an XSLT template's logic against an XML schema?

An XSLT transform makes implicit assumptions about the structure of the XML that it is transforming. XSLT转换对要转换的XML结构进行隐式假设。 For example, the following: 例如,以下内容:

<xsl:variable name="b"><xsl:value-of select="A/B"/></xsl:variable>

Assumes that the XML node "B" sits directly underneath "A", such as: 假定XML节点“ B”位于“ A”的正下方,例如:

<A>
  <B>Hello</B>
<\A>

If the XML format has changed, and becomes: 如果XML格式已更改,并且变为:

<A>
  <AA>
    <B>Hello</B>
  </AA>
<\A>

The XSLT will now fail to find node B, and will assume that the node hadn't been specified in this case (which would be a valid option). XSLT现在将无法找到节点B,并且将假定在这种情况下未指定该节点(这是一个有效的选项)。

I had assumed that it would be possible to specify the XML schema that the XSLT is attempting to transform, and if the XSLT ever referenced a node that the schema didn't recognise, a validation error would be raised. 我以为可以指定XSLT尝试转换的XML模式,并且如果XSLT曾经引用过该模式无法识别的节点,则将引发验证错误。 However, I can't seem to find this implementation ( https://www.ibm.com/developerworks/library/x-schemaxslt/index.html appears to mention only validating the input XML and the generated XML - not the XSLT itself). 但是,我似乎找不到这种实现( https://www.ibm.com/developerworks/library/x-schemaxslt/index.html似乎只提到了验证输入XML和生成的XML,而不是XSLT本身)。

[Although an approach might be to create a XML schema of what the XSLT is expecting, and then compare the input XML against it, I have 100's of XSLTs at different versions, so this is impractical. [尽管一种方法可能是创建XSLT期望的XML模式,然后将输入的XML与之比较,但我有100个不同版本的XSLT,因此这是不切实际的。 It is simple, however, to create an XSD of the input and ask the XSLT if it is OK with that.] 但是,创建输入的XSD并询问XSLT是否可以这样做很简单。

This is what schema-aware processing in XSLT 2.0/3.0 is designed to achieve. 这就是XSLT 2.0 / 3.0中可识别模式的处理的目的。

In the match pattern of a template rule, you have to declare that you are only going to use it to process schema-valid elements: 在模板规则的匹配模式中,您必须声明仅将使用它来处理架构有效的元素:

<xsl:template match="schema-element(invoice)">
 ...
</xsl:template>

or in XSLT 3.0 you can declare that all the template rules in a mode are designed to handle schema-valid input: 或在XSLT 3.0中,您可以声明某个模式下的所有模板规则都旨在处理架构有效的输入:

<xsl:mode typed="strict"/>

and then if you use a path that can't exist according to the schema, for example 然后如果您根据架构使用了不存在的路径,例如

<xsl:template match="schema-element(invoice)">
 <xsl:apply-templates select="customer-detials"/>
</xsl:template>

the XSLT processor will tell you about it. XSLT处理器将告诉您有关情况。 You have to tell the XSLT processor where to find the schema using an <xsl:import-schema> declaration. 您必须使用<xsl:import-schema>声明告诉XSLT处理器在哪里可以找到模式。

Schema-awareness is a vastly underused feature of the language. 模式意识是该语言的一个未被充分利用的功能。 It takes a bit of effort up-front to take advantage of it, and it can sometimes be a bit of a hassle with "false positive" error messages, but once you get the hang of it, it can catch a great number of simple user errors that would otherwise take hours to debug. 要利用它,需要先付出一些努力,并且有时可能会因“假阳性”错误消息而有些麻烦,但是一旦掌握了这些错误,它就可以抓住很多简单的方法用户错误,否则将需要数小时来调试。 In some cases schema-awareness can also aid performance, by reducing the amount of the source document that needs to be searched, and by avoiding repeated datatype conversions -- but better static error checking is the main benefit. 在某些情况下,模式感知还可以通过减少需要搜索的源文档的数量以及避免重复的数据类型转换来帮助提高性能,但是更好的静态错误检查是主要的好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM