I have to validate an XML with an XSD.
The XML could look like this:
<content>
<uuid>1234</uuid>
<type>group1</type>
... some more elements
</content>
The XML could also look like this:
<content>
<uuid>asdf</uuid>
<type>group2</type>
... some other elements which may differ from the first XML
</content>
In the first XML, the uuid is of type xs:integer
. In the second XML, the uuid is of type xs:string
.
To validate these XMLs in an XSD, I decided to use groups
within a choice
.
My XSD looks like this:
<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>
With XMLSpy, I get following error:
Element 'uuid' is not consistent with element 'uuid'.
Yes, they are not consistent, but that is exactly what I want to have :-)
So, how do I have to change the XSD, so that I could use the same element(s) with different types in different groups but in the same choice? The uuid is not the only element, which might differ, that's why I implemented the group
-solution.
Thanks for helping!
EDIT To bypass the ambiguity of uuid, the order in this example isn't important. <uuid> can be e.g. the last element, too.