【发布时间】:2015-04-04 16:46:58
【问题描述】:
XML 有一个父元素 parent,它必须包含 10 个子元素:child1 到 child10,顺序不限,但只有一次。
作为替代方案,parent 元素可以选择包含 child11 到 child20,而不是 child1 到 child10。
<parent><child1/><child4/> ... <child2/></parent> OK
<parent><child15/>...<child20/></parent> OK
<parent><child1/><child2/></parent> BAD, missing childs
<parent><child1/>...<child11/></parent> BAD: child11 shall never be with child1
<parent><child1/><child1/></parent> BAD: childs shall not be repeated
这是我迄今为止的架构定义尝试:
<xs:complexType name="parent">
<xs:choice>
<xs:sequence>
<xs:element name="child1" type="xs:integer" minOccurs="1" maxOccurs="1"/>
<xs:element name="child2" type="xs:integer" minOccurs="1" maxOccurs="1"/>
...
</xs:sequence>
<xs:sequence>
<xs:element name="child11" type="xs:integer" minOccurs="1" maxOccurs="1"/>
<xs:element name="child12" type="xs:integer" minOccurs="1" maxOccurs="1"/>
...
</xs:sequence>
</xs:choice>
</xs:complexType>
【问题讨论】:
-
为什么
child1不能和child11一起(例子4)而child5可以和child20一起(例子2)? -
@kjhughes 好地方我没注意到,我认为这是一个错字
-
是的,很抱歉,现在解决了
-
这似乎是一个非常常见/有用的 XSD 用例,为什么这不是方案语言的一部分?我是否错误地使用了 XML?
-
不,你的要求是完全合理的。这是 XML Schema 语言的一个薄弱环节。 XML Schema 非常强调顺序内容(例如,对于复杂类型继承,您只能按顺序附加到基本类型内容)。这确实让那些顺序在 XML 格式中不重要的数据变得困难,这些格式旨在(在某种程度上)供人类读写。