user6457056
user6457056

Reputation:

how to put XSD restriction on a group of attributes

I have following XSD.

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="b">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element maxOccurs="unbounded" name="c">
                            <xs:complexType>
                                <xs:attribute name="id" use="required" >
                                    <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                            <xs:enumeration value="1"/>
                                            <xs:enumeration value="2"/>
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                                <xs:attribute name="name" use="required" >
                                    <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                            <xs:enumeration value="AA"/>
                                            <xs:enumeration value="BB"/>
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

That's valid for

<?xml version="1.0" encoding="UTF-8" ?>
<a>
    <b>
        <c id="1" name="AA"/>
        <c id="2" name="BB"/>
    </b>
</a>

but I want to write a Schema Document that restrict the values in the combination of all attributes. For more clarification the above XSD is also valid for following XMLs.

<?xml version="1.0" encoding="UTF-8" ?>
<a>
    <b>
        <c id="2" name="AA"/>
        <c id="2" name="BB"/>
    </b>
</a>

<?xml version="1.0" encoding="UTF-8" ?>
<a>
    <b>
        <c id="1" name="AA"/>
        <c id="1" name="BB"/>
    </b>
</a>

<?xml version="1.0" encoding="UTF-8" ?>
<a>
    <b>
        <c id="1" name="BB"/>
        <c id="2" name="BB"/>
    </b>
</a>

But I want to restrict it to the group values like, it should be valid for first example, but not for any other xml. Is there any way to do that?

<?xml version="1.0" encoding="UTF-8" ?>
<a>
    <b>
        <c id="1" name="AA"/>
        <c id="2" name="BB"/>
    </b>
</a>

Upvotes: 2

Views: 259

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

I'm not very good at working out general rules from a small number of examples. But the general principle is that to write rules for combinations of attributes, you need XSD 1.1 assertions. There are several schema processors that support XSD 1.1 and assertions, but there are also quote a few that don't, so you have to make a decision.

Upvotes: 1

Related Questions