How to define a user define data type in XML schema?
The way to use a common simple type for the developer id is to declare it as a named type at the beginning:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:simpleType name="developerID"> <xs:restriction base="xs:string"> <xs:length value="5"/> <xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/> </xs:restriction> </xs:simpleType> . . .
and then use it:
. . . <xs:element name="Developer" maxOccurs="unbounded"> <xs:complexType> <xs:all> <xs:element name="ID" type="developerID"> . . . <xs:element name="App" maxOccurs="unbounded"> <xs:complexType> <xs:all> . . . <xs:element name="Developer" type="developerID"/>
But this is not enough to create a contraint so that appinfo/App/Developer
can contain only one of the developers' ids declared in appinfo/Developer/ID
. To do that is necessary to create a unique key definition using xs:key
and reference it using xs:keyref
(see here).
Here is the complete XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:simpleType name="developerID"> <xs:restriction base="xs:string"> <xs:length value="5"/> <xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/> </xs:restriction> </xs:simpleType> <xs:element name="appinfo"> <xs:complexType> <xs:sequence> <xs:element name="Developer" maxOccurs="unbounded"> <xs:complexType> <xs:all> <xs:element name="ID" type="developerID"> </xs:element> <xs:element name="Name"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-zA-Z])+"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="Email"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[^@]+@[^\.]+\..+"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="Company" type="xs:string"/> <xs:element name="Website" type="xs:string"/> <xs:element name="Phone"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="13"/> </xs:restriction> </xs:simpleType> </xs:element> </xs:all> </xs:complexType> </xs:element> <xs:element name="App" maxOccurs="unbounded"> <xs:complexType> <xs:all> <xs:element name="ID"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="5"/> <xs:pattern value="[0-9][0-9][0-9][0-9][A-Z]"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="Name"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-zA-Z])+"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="Developer" type="developerID"/> <xs:element name="Price"> <xs:simpleType> <xs:restriction base="xs:float"> <xs:minInclusive value="0.00"/> <xs:maxInclusive value="6.99"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="TabletCompatible" type="xs:boolean" minOccurs="0"/> <xs:element name="Category"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Travel"/> <xs:enumeration value="Productivity"/> <xs:enumeration value="Game"/> <xs:enumeration value="Music"/> <xs:enumeration value="Education"/> <xs:enumeration value="Lifestyle"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="Platform"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Android"/> <xs:enumeration value="iOS"/> <xs:enumeration value="Blackberry"/> </xs:restriction> </xs:simpleType> </xs:element> </xs:all> </xs:complexType> <xs:keyref name="developerIDref" refer="developerID"> <xs:selector xpath="."/> <xs:field xpath="Developer"/> </xs:keyref> </xs:element> <xs:element name="AppStat"> <xs:complexType> <xs:all> <xs:element name="AppID" type="xs:anySimpleType"/> <xs:element name="Statistics"> <xs:complexType> <xs:sequence maxOccurs="unbounded"> <xs:element name="Platform" type="xs:anySimpleType"/> <xs:element name="Downloads" type="xs:positiveInteger"/> <xs:element name="Rating"> <xs:simpleType> <xs:restriction base="xs:float"> <xs:minInclusive value="0.0"/> <xs:maxInclusive value="5.0"/> </xs:restriction> </xs:simpleType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="LastChecked" type="xs:date"/> </xs:all> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> <xs:key name="developerID"> <xs:selector xpath="Developer"/> <xs:field xpath="ID"/> </xs:key> </xs:element></xs:schema>