Choice with similar sequences

33 Views Asked by At

I have this xsd file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    targetNamespace="http://HIDDEN/Queues"
    xmlns="http://HIDDEN/Queues">

    <xs:complexType name="Queue">
        <xs:sequence>
            <xs:choice>
                <xs:sequence>
                    <xs:element fixed="1" name="id" type="xs:long"/>
                    <xs:element fixed="a" name="name" type="xs:string"/>
                </xs:sequence>
                <xs:sequence>
                    <xs:element fixed="2" name="id" type="xs:long"/>
                    <xs:element fixed="b" name="name" type="xs:string"/>
                </xs:sequence>
            </xs:choice>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

The goal is that an element with the type Queue should be able to look like this:

<Queue>
   <id>1</id>
   <name>a</name>
</Queue>

but as soon as either id or name do not match togehter (e.g id=2 name=a) it should fail.

The part im confused abt is this error:

"http://HIDDEN/Queues":id and "http://HIDDEN/Queues":id (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.

I mean i see what its referencing to but those are elements that cant exist at the same time so idk how to fix that. One idea i had was to jus make id and name as attributes but my boss said i cant do that (dont ask me why).

Do any of you know any other approaches? thanks in advance!

1

There are 1 best solutions below

0
Yitzhak Khabinsky On

Here is a solution based on XSD 1.1 that is using an assert statement to enforce correct combinations of id and name values.

Input XML

<Queue xmlns="http://HIDDEN/Queues">
    <id>1</id>
    <name>a</name>
</Queue>

XSD 1.1

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified" targetNamespace="http://HIDDEN/Queues"
           xmlns="http://HIDDEN/Queues"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">
    <xs:element name="Queue">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="id"/>
                <xs:element ref="name"/>
            </xs:sequence>
            <xs:assert test="string-join((xs:string(id), name), '~') = ('1~a', '2~b')"
                       xpathDefaultNamespace="##targetNamespace">
                <xs:annotation>
                    <xs:documentation>Rule #: 20-70</xs:documentation>
                    <xs:documentation>Combination of id and name values shall be correct</xs:documentation>
                </xs:annotation>
            </xs:assert>
        </xs:complexType>
    </xs:element>
    <xs:element name="id" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
</xs:schema>