Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD: How to restrict enumeration values of a derived complex type?

Given the following example:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="Book" abstract="true">
        <xs:sequence>
            <xs:element name="titel" type="xs:string">
            </xs:element>
            <xs:element name="bookCode" type="BookEnum"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Lyric">
        <xs:complexContent>
            <xs:extension base="Book">
                <xs:sequence>
                    <xs:element name="author" type="xs:string">
                    </xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:simpleType name="BookEnum">
        <xs:restriction base="xs:int">
            <xs:enumeration value="Paperback"/>
            <xs:enumeration value="Hardcover"/>
            <xs:enumeration value="Liporello"/>
            <xs:enumeration value="None"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

Lyric derives from Book. I would like to restrict the possible values of the BookEnum for the complexType "Lyric" to be "Paperback".

"None", "Liporello" and "Hardcover" should no longer be valid values for "Lyric". can this be done in xsd?

like image 759
tobsen Avatar asked Feb 15 '10 12:02

tobsen


People also ask

How do I restrict attribute values in XML schema?

Restrictions on a Set of Values To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint. Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element.

What is enumeration value in XSD?

Enumerations are a base simple type in the XSD specification containing a list of possible values. Single-valued enumerations are shown as restrictions of the base simple type xs:token , as illustrated below: ? < xs:simpleType name=”GraduationPlanTypeMapType”>

What is simpleType and complexType in XSD?

XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.

What is XS complexType in XSD?

Definition and UsageThe complexType element defines a complex type. A complex type element is an XML element that contains other elements and/or attributes.


2 Answers

Nope, unfortunately you cannot do that. There is no way to restrict an enumeration like that, and you also would have trouble both extending and restricting a complex type at the same time. There is a good article what you can do with enumerations here.

Maybe consider working bottom-up instead: define a basic book type with almost nothing in it; then define a LyricType that is a union of the basic type and some more values; carry the bookCode in the sub-types. It's not ideal, but alas, XSD does not always align with object-oriented languages.

like image 68
xcut Avatar answered Sep 21 '22 20:09

xcut


Ultimately .. you are trying to validate a node using value of the other (Parent or sibling or etc) .. Which is certainly not possible ..

like image 24
InfantPro'Aravind' Avatar answered Sep 18 '22 20:09

InfantPro'Aravind'