3

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Tom
  • 33
  • 4
  • 1
    [This answer](http://stackoverflow.com/a/31723079/1987598) could be helpful advice for you. Having two definitions for `uuid` in this way creates ambiguity. – Mathias Müller Feb 10 '16 at 14:35
  • If you were doing your XML validation yourself, manually: How would you decide whether to interpret an occurrence of `uuid` as a string or integer? – Mathias Müller Feb 10 '16 at 14:41
  • The ambiguity in the answer, you mentioned, is, because the order of the elements are important. In my case, the order isn't important, but the type is. – Tom Feb 10 '16 at 14:42
  • This is the second task: In dependence of the type-element, a group should be used – Tom Feb 10 '16 at 14:47
  • What do you mean by "second task"? Please answer the question in my second comment. – Mathias Müller Feb 10 '16 at 14:53
  • The goal of the XSD is to validate in dependence of the -element some other elements. When the is e.g. group1, then the elements of group1 should be validated. The elements of group1 and group2 could be the same, but they could also differ from each other. When I should do the XML validation manually, the decision depend on the element – Tom Feb 10 '16 at 14:58
  • 2
    How about making a single definition for `uuid` and the like, which is general enough to account for all cases? Then use assertions to check additional constraints that have to do with the value of the `type` element. – Mathias Müller Feb 10 '16 at 16:02
  • If you don't want to follow @MathiasMüller suggestion and make `uuid` be a `xs:string` and generally merge the two types into a general case, you might prefer to use [Conditional Type Assignment](http://www.w3.org/TR/xmlschema11-1/#cTypeAlternative), assuming you're willing to change `type` to an attribute. (CTA won't work with it as an element.) See [**How to make type depend on attribute value using Conditional Type Assignment**](http://stackoverflow.com/q/27878402/290085). – kjhughes Feb 10 '16 at 18:05
  • Hi all, the 'uuid' is just an example. I already used CTA, but in this case, it would be a huge overheading, because with CTA, I would have to decide for every single element, which group I want to use. It would be much nicer to do the other way round: To choose a group and define the elements in the group – Tom Feb 11 '16 at 11:08
  • If you either 1) misrepresent your actual problem in your question or 2) are not willing to accept one of our recommendations, there is little point in asking here. – Mathias Müller Feb 11 '16 at 16:44

1 Answers1

2

Any two elements with the same name and the same parent must have the same type in XSD. That is the essence of the 'Element declarations consistent' constraint. From the wording of the error, I believe XML Spy is telling you that you violated that constraint. (You also violated the 'Unique Particle Attribution' constraint.

If you want your uuid element to accept either strings or integers, assign it a type which is the union of integer and string. The order is important, since the first to match determines how the element value will be typed in the post-schema-validation infoset.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65