Jagmohan Singh
Jagmohan Singh

Reputation: 1

Changing restriction in XSD based on other element value

I have an element "Method" and "Name" as given below: I want to change the ref in "Name" from "group1" to "group2" based on value in "Method" as I want to change the restrictions.

Is it possible to do that?

<xs:element name="Method" type="xs:string" />

<xs:element name="Name">
    <xs:complexType>
        <xs:sequence>
            <xs:choice maxOccurs="unbounded">
                <xs:element ref="group1" />
            </xs:choice>
        </xs:sequence>
    </xs:complexType>
</xs:element>


<xs:element name="group1">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="ABC" />
            <xs:enumeration value="PQR" />
        </xs:restriction>
    </xs:simpleType>
</xs:element>

<xs:element name="group2">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="XYZ" />
            <xs:enumeration value="ABC" />
        </xs:restriction>
    </xs:simpleType>
</xs:element>

Upvotes: 0

Views: 675

Answers (1)

kjhughes
kjhughes

Reputation: 111491

For XSD 1.0, you'll have to validate such a constraint outside of XML Schema. Consider using XSLT for the extra validation or Schematron.

For XSD 1.1, you can use conditional type assignment. See xs:alternative. Note, however, that you'll have to restructure somewhat. The xs:alternative tests can refer to attributes on the element itself and constants, not to arbitrary elements within the document; it's not even allowed to reference other elements or attributes relatively.

Upvotes: 1

Related Questions