问题描述
有 XML
<a>
<b info="Info1">
<c info="Info2" />
<d info="Info3" />
</b>
<b info="Info4">
<e info="Info5" />
<f info="Info6" />
</b>
</a>
两个块都带有
<b info=...>...</b>
元素可能会或可能不会出现在 XML 中,这取决于某些未知的外部条件。因此可以使用 4 种类型的 XML 进行验证:
<a/>
<a><b info="Info1"><c info="Info2" /><d info="Info3" /></b></a>
<a><b info="Info4"><e info="Info5" /><f info="Info6" /></b></a>
<a><b info="Info1"><c info="Info2" /><d info="Info3" /></b><b info="Info4"><e info="Info5" /><f info="Info6" /></b></a>
<element name="a">
<complexType>
<sequence>
<sequence minOccurs="0">
<element name="b">
<annotation>
<documentation>
<info>Info1</info>
</documentation>
</annotation>
<complexType>
<sequence>
<element name="c">
<annotation>
<documentation>
<info>Info2</info>
</documentation>
</annotation>
</element>
<element name="d">
<annotation>
<documentation>
<title>Info3</title>
</documentation>
</annotation>
</element>
</sequence>
</complexType>
</element>
</sequence>
<sequence minOccurs="0">
<element name="b">
<annotation>
<documentation>
<info>Info4</info>
</documentation>
</annotation>
<complexType>
<sequence>
<element name="e">
<annotation>
<documentation>
<info>Info5</info>
</documentation>
</annotation>
</element>
<element name="f">
<annotation>
<documentation>
<title>Info6</title>
</documentation>
</annotation>
</element>
</sequence>
</complexType>
</element>
</sequence>
</sequence>
</complexType>
</element>
如果嵌套块不包含同名的顶级元素,这样的模式将完美地描述所有可能的 XML 变体。
但是这样的方案是无效的,因为在同一层有两个同名的“b”块。由于这些块包含必须包含在“注释”块中的不同信息属性,因此它们不能合并为一个。
如何为所有可能的情况制定这样的方案?
解决方法
你没有说清楚实际规则是什么,你只是给我们展示了一些例子。
但是 XSD 有一个绝对规则,如果两个同级元素具有相同的名称,那么它们必须具有相同的声明类型。