本文介绍了在 XSD 中,我可以根据父元素的内容设置强制子元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML &XSD 耦合,其中包含类似于以下内容的House"元素:

I have an XML & XSD coupling which includes 'House' elements similar to the following:

<root>
...
<House>
    <id>1</id>
    <type>Cottage</type>
    <imageSrc> image.jpg </imageSrc>
</House>
<House>
    <id>2</id>
    <type>Private</type>
</House>

我想通过我的 XSD 强制要求所有类型为Cottage"的房子都必须有一个强制性标签,但其他房子没有.(或者反过来说——唯一的私人".有没有办法做到这一点?

I want to enforce via my XSD that all Houses of type "Cottage" must have a mandatory tag, but other houses do not. (Or the other way aruond - the only 'Privates' do not. Is there a way to do this?

推荐答案

您可能需要考虑使用诸如 Schematron 表达这些超出 XML Schema 能力的附加验证规则.Schematron 是一个开放 (ISO) 标准,它允许您表达关于应该出现在 XML 文档中的模式的断言.例如,您可以使用它来表达约束,例如:

You might want to consider using something like Schematron to express these additional validation rules that are beyond the capabilities of XML Schema. Schematron is an open (ISO) standard which allows you to express assertions about patterns that should be present in XML documents. For example, you can use it to express constraints such as:

  • 如果元素 A 存在,则元素 B 被禁止.如果 AB 是兄弟,您可以使用 XML 模式中的 xs:choice 来做到这一点,但这种方法在 XML 中不起作用如果两个元素不是兄弟元素,则为架构.
  • 如果元素 A 具有具有特定值的属性,则它必须包含子元素 B.
  • 元素 A 必须等于元素 BC 的总和.
  • 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 是强制性标签,您希望在Cottage"类型的房屋上强制使用该标签并禁止其他人使用.

In your specific example, I'm going to assume that imageSrc is the mandatory tag whose presence you want to mandate on Houses of type 'Cottage' and prohibit on others.

表达这些断言的简单 Schematron 模式的示例是:

An example of a simple Schematron schema to express these assertions is:

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

在这里,我将模式表示为两个单独的规则,一个用于您的需求的两个部分中的每一个.

Here, I've expressed the pattern as two separate rules, one for each of the two parts of your requirement.

每个 rule 都有一个 context 属性,它是一个 XPath 表达式.当这个表达式的计算结果为真时,规则触发".因此,第一条规则在 House 元素上触发,其 type 子元素的文本内容等于 'Cottage'.

Each rule has a context attribute which is an XPath expression. When this expression evaluates to true, the rule 'fires'. So, the first rule fires on House elements with a type child element whose text content is equal to 'Cottage'.

当规则触发时,然后通过评估 assert 元素的 test 属性来检查断言.同样,这是一个 XPath 表达式,但用于评估测试表达式的上下文节点是与 rule 元素(在本例中为 House)匹配的节点.当测试表达式的计算结果为真时,断言通过(即文档对于这个断言是有效的).如果计算结果为 false,则断言失败.assert 元素的文本内容通常是人类可读的断言描述,可以传递给最终用户.Schematron 还提供了额外的工具(称为诊断")来将额外的技术消息链接到断言.

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.

可在 http://www.schematron.com 上获得公开可用的基于 XSLT 的 Schematron 实现/implementation.html.典型的验证管道将首先根据定义文档基本结构要求(例如元素层次结构、基数、数据类型、模式等)的 XML 模式验证文档.然后,一旦文档通过了这个基本级别的验证,您就可以根据 Schematron 模式验证该文档,以验证其他基于模式的约束.该管道可以通过编程方式实现,或者使用(例如)批处理文件、Ant 脚本或 XProc 处理器.

A publicly-available XSLT-based implementation of Schematron is available at http://www.schematron.com/implementation.html. A typical validation pipeline would involve first validating the document against an XML Schema that defines the basic structural requirements for the document (e.g. element hierarchy, cardinalities, data types, patterns etc). Then, once the document has passed this basic level of validation, you would then validate the document against the Schematron schema to validate the additional pattern-based constraints. This pipeline could be implemented programatically, or using (for example) batch files, Ant scripts or an XProc processor.

这篇关于在 XSD 中,我可以根据父元素的内容设置强制子元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:24