这里有一些解决方案:
使用选择
这是一个很长的解决方案,因为它重复了模型的某些部分,但如果您确实需要可选标签之间的强制标签并且标签不在xs:all 内,它就可以工作:
<xsd:choice>
<!-- Choice option 1: optional tags present -->
<xsd:sequence>
<xsd:element name="optionalTag1"/>
<xsd:element name="complulsoryTag1"/>
<xsd:element name="complulsoryTag2"/>
<xsd:element name="optionalTag2"/>
</xsd:sequence>
<!-- Choice option 2: optional tags not present -->
<xsd:sequence>
<xsd:element name="complulsoryTag1"/>
<xsd:element name="complulsoryTag2"/>
</xsd:sequence>
</xsd:choice>
请注意,如果您使用xs:group 对您的complusory 中心标签进行分组,您可以避免在模型上重复标签。
以可选顺序包装标签
如果它们之间没有强制标签,您可以简单地将它们包装成带有minOccurs=0 的序列。因此,如果序列出现,则两个标签都存在,如果序列没有出现,则没有任何标签存在:
<xsd:sequence minOccurs="0">
<xsd:element name="tag1"/>
<xsd:element name="tag2"/>
</xsd:sequence>
请注意,这在 xs:all 中不起作用,但您可以在选择中使用它,如果需要,甚至可以在另一个序列中使用它。
使用 XSD 1.1 断言
如果您的处理器使用 XSD 1.1,您可以使用 xs:assert 确保所有可选标签都存在或都不存在:
<xsd:complexType>
<xsd:sequence>
<xsd:element name="optionalTag1" minOccurs="0"/>
<xsd:element name="complulsoryTag1"/>
<xsd:element name="complulsoryTag2"/>
<xsd:element name="optionalTag2" minOccurs="0"/>
</xsd:sequence>
<!-- Both optional tags are present or none of them are present -->
<xsd:assert test="boolean(optionalTag1) = boolean(optionalTag2)"/>
</xsd:complexType>
请注意,这是提出的唯一一个也适用于xs:all 的解决方案。