问题描述
这是一个与 如何为 XML 节点的无序列表创建模式,具有出现约束,但实际上稍微简单一些.但是,我很难理解序列和选择背后的逻辑(尤其是当它们嵌套到选择序列或序列选择中时),尽管我已经研究了很长时间,但我无法理解上面的示例有效.
This is a similar question to How to create a schema for an unordered list of XML nodes, with occurrence constraints, but actually slightly simpler. However I am having great trouble understanding the logic behind sequences and choices (and especially when they are nested into sequences of choices or choices of sequences), and although I've studied it for a long time I can't understand how the example above works.
我需要的是节点列表 ChildA 和 ChildB 的架构,其中 ChildA 可以出现 0-n 次,但 ChildB 只能出现 0-1 次.(实际上我需要每种类型的几个节点,但如果我可以为 ChildA 和 ChildB 做,将它扩展到 ChildX 等和 ChildY 等应该很简单).不应该有顺序限制.我会很感激任何帮助.任何深入解释架构指标的链接也会有所帮助.
What I need is schema for a list of nodes, ChildA and ChildB, whereby ChildA can occur 0-n times, but ChildB only 0-1 times. (Actually I need several nodes of each type, but if I can do it for ChildA and ChildB, extending it to ChildX etc. and ChildY etc. should be simple). There should be no order constraint. I'd appreciate any help. Any links that explain schema indicators in depth would also be helpful.
推荐答案
这将是我很快想到的最简单的解决方案.这里的关键是在主"序列中使用另一个序列.通过将内部序列设置为以 <ChildB>
开头,模式保持确定性,并且通过将该序列的基数设置为 0-1,<ChildB>
保持可选.
This would be the simplest solution that quickly came to my mind. The key point here is to use another sequence inside the "main" sequence. The schema is kept deterministic by setting the inner sequence to start with <ChildB>
and <ChildB>
is kept optional by setting the cardinality of that sequence to 0-1.
这是一个 XMLSchema 1.0 解决方案.
This is an XMLSchema 1.0 solution.
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Schema for elements ChildA and ChildB
The requirements are as follows:
* ChildA and ChildB may occur in any order.
* ChildA is optional and may occur multiple times.
* ChildB is optional and may occur once only.
-->
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="AB-container" type="AB-type" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="AB-type">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="ChildA" type="xs:string" />
<xs:sequence minOccurs="0">
<xs:element name="ChildB" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="ChildA" type="xs:string" />
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:schema>
这篇关于如何为无序列表创建一个模式,其中一些出现一次,一些出现多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!