我必须使用 XSD 验证 XML。
XML 可能如下所示:
<content>
<uuid>1234</uuid>
<type>group1</type>
... some more elements
</content>
XML 也可能如下所示:
<content>
<uuid>asdf</uuid>
<type>group2</type>
... some other elements which may differ from the first XML
</content>
在第一个 XML 中,uuid 的类型是xs:integer
。在第二个 XML 中,uuid 的类型为xs:string
。
为了在 XSD 中验证这些 XML,我决定groups
在choice
.
我的 XSD 看起来像这样:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="content">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:group ref="group1"/>
<xs:group ref="group2"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:group name="group1">
<xs:sequence>
<xs:element name="uuid" type="xs:integer"/>
... some more elements
</xs:sequence>
</xs:group>
<xs:group name="group2">
<xs:sequence>
<xs:element name="uuid" type="xs:string"/>
... some more elements which may differ from the first XML
</xs:sequence>
</xs:group>
</xs:schema>
使用 XMLSpy,我收到以下错误:
Element 'uuid' is not consistent with element 'uuid'.
是的,它们并不一致,但这正是我想要的 :-)
那么,我该如何更改 XSD,以便我可以在不同组中使用具有不同类型但在一样的选择?uuid 不是唯一的元素,可能会有所不同,这就是我实施group
-solution 的原因。
感谢您的帮助!
编辑 要绕过 uuid 的歧义,此示例中的顺序并不重要。<uuid> 也可以是例如最后一个元素。