Reputation: 95
The following schema defines a 'book' element with two attributes i.e., 'DEPARTMENT' and 'BOOK_NAME'. I want to improve the schema so that while writing a corresponding XML file, the 'DEPARTMENT' attribute appears first, it's value be selected (PHOTOGRAPHY/COMPUTER_SCIENCE/MEDICINE) and then based on the value of the 'DEPARTMENT' attribute, a 'BOOK_NAME' is selected from the corresponding list. So, if DEPARTMENT="PHOTOGRAPHY", the author of the XML file shouldn't be able to select a book from COMPUTER_SCIENCE or MEDICINE department.
Please don't suggest to split the book element and have these attributes as child elements, I'm looking for a solution that restricts the type of one attribute based on the type selected for another attribute.
Thank you
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element ref="book"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:attribute name="DEPARTMENT" type="departmentName" use="required"/>
<xs:attribute name="BOOK_NAME" type="should be photographyBooks or computerScienceBooks or medicineBooks depending on the selected department" use="required"/>
</xs:complexType>
</xs:element>
<xs:simpleType name="departmentName">
<xs:restriction base="xs:string">
<xs:enumeration value="PHOTOGRAPHY"/>
<xs:enumeration value="COMPUTER_SCIENCE"/>
<xs:enumeration value="MEDICINE"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="photographyBooks">
<xs:restriction base="xs:string">
<xs:enumeration value="Adobe PhotoShop in a nutshell"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="computerScienceBooks">
<xs:restriction base="xs:string">
<xs:enumeration value="An Intruduction to Computer Programming using C"/>
<xs:enumeration value="Best Practices in Java"/>
<xs:enumeration value="Guide to Perl Scripting"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="medicineBooks">
<xs:restriction base="xs:string">
<xs:enumeration value="Principles an Practices of Medicine"/>
<xs:enumeration value="Mortality in relation to Smoking"/>
<xs:enumeration value="Heart Protection Study"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Upvotes: 0
Views: 334
Reputation: 25034
XSD 1.0 is not designed to express constraints of this kind.
If you want to work with XSD 1.0, you don't want this design for your XML. If you want this XML structure, you don't want to work with XSD 1.0. Alternatives include XSD 1.1 (using assertions or conditional type assignment) or Relax NG or Schematron.
Upvotes: 2