您不妨考虑使用Schematron等内容来表达这些超出XML Schema能力的额外验证规则。 Schematron是开放式(ISO)标准,允许你就XML文件中应当存在的模式表示意见。 例如,你可以利用它来表达制约因素,例如:
- if element
A
is present, element B
is prohibited. If A
and B
are siblings, you could do this using an xs:choice
in XML schema, but this approach doesn t work in XML Schema if the two elements aren t siblings.
- if element
A
has an attribute with a specific value, then it must contain child element B
.
- element
A
must be equal to the total of elements B
and C
.
在你的具体例子中,我会假设,<代码>imageSrc是强制性的标签,你想要在家庭之家任职,并对他人加以禁止。
一个简单的Schematron schema来表达这些说法的例子就是:
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<pattern>
<!-- Houses of type Cottage must contain an imageSrc element -->
<rule context = "/root/House[type/text() = Cottage ]">
<assert test = "imageSrc">Cottages must have an image</assert>
</rule>
<!-- Houses not of type Cottage must not contain an imageSrc element -->
<rule context = "/root/House[type/text() != Cottage ]">
<assert test = "not(imageSrc)">Non-cottages must not have an image</assert>
</rule>
</pattern>
</schema>
我在此表示,这种模式是两项单独的规则,是你要求的两个部分中的每一个部分。
每一条<代码>>>>>>> >条/代码>都有一个<代码>context属性,即XPath表述。 当这一表述真实时,规则火力就会消失。 因此,在<代码>House <>/code>中带有<条码>类型条码>的儿童部分的第1条规则火灾,其文本内容等同于家庭。
When the rule fires, the assertion is then checked, by evaluating the test
attribute of the assert
element. Again, this is an XPath expression, but the context node for evaluating the test expression is the node matched by the rule
element (in this case House
). When the test expression evaluates to true, the assertion passes (i.e. the document is valid with respect to this assertion). If it evaluates to false, the assertion fails. The text content of the assert
element is typically a human-readable description of the assertion that could be passed onto end users. Schematron also offers additional facilities (called diagnostics ) to link additional technical messages to assertions.
可在上查阅。 典型的验证管道将首先对XML Schema文件进行验证,该文件界定了该文件的基本结构要求(例如要素等级、心脏、数据类型、模式等)。 然后,一旦文件达到这一基本验证水平,你就会对Schematron schema的文件进行验证,以证实基于模式的其他限制。 这一管道可按方案实施,或(例如)使用批量文档、字母或XProc处理器。