1

我有一个简单的 XML 结构,如下所示

...
<param>
  <name>foo</name>
  <type1>
     Some complex content
  </type1>
</param>
<param>
  <name>bar</name>
  <type2>
     Some complex content
  </type2>
</param>
...

我使用的架构看起来像这样

...
<xs:complexType name="par">
  <xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element ref="typeSpecific"/>
  </xs:sequence>
</xs:complexType>

<xs:element name="typeSpecific" abstract="true"/>
<xs:element name="type1" type="t1" substitutionGroup="typeSpecific"/>
<xs:element name="type2" type="t2" substitutionGroup="typeSpecific"/>
...

我想扩展它,如果我在参数中使用 type1,我只能有一个这样的元素,但如果我使用 type2,我可以有很多,像这样:

...
<param>
  <name>bar</name>
  <type2>
     Some complex content
  </type2>
  ...
  <type2>
     Some other complex content
  </type2>
</param>

type2由于兼容性问题,我想避免将元素包装在容器中。我的第一个冲动是使用maxOccurs="unbounded"属性<xs:element name="type2" type="t2" substitutionGroup="typeSpecific" maxOccurs="unbounded"/>,但这是不允许的。

有没有办法使用我当前的结构对 XSD 强制执行此限制?我可以以某种方式使用<xs:choice>而不是替代来实现这一目标吗?

4

1 回答 1

0

XSD 1.0 不允许一个元素的类型依赖于另一个元素的内容。

您可以使用断言在 XSD 1.1 中实现此类依赖关系。

编辑: 您不能使用替换组执行此操作:如果元素可以相互替换,则两者的出现约束必须相同。您将需要一个带有选项的显式内容模型。

于 2013-07-11T12:17:15.097 回答