Designing Your own XML Schema: Restrictions and User Defined Types - User-defined Complex Type in XML Schema
(Page 7 of 7 )
In the previous sections, we dealt only with simple user-defined types. Now, we shall deal with the user-defined complex type.
Let us consider that I would like to define my own data type called “AddressType,” which holds Street, City and Zip elements. After defining “AddressType,” I would like to use it with two elements, “ResAddress” (residential address) and “OffAddress” (Office Address) as part of the “Employee” definition. The following listing of a complete XML Schema does the same:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault=
"unqualified">
<xs:element name="Employees">
<xs:annotation>
<xs:documentation>Contains All Employee information</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:string">
</xs:element>
<xs:element name="Name" type="xs:string" />
<xs:element name="ResAddress" type="AddressType" />
<xs:element name="OffAddress" type="AddressType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="PK_Employee_ID">
<xs:selector xpath=".//Employee" />
<xs:field xpath="ID" />
</xs:key>
</xs:element>
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="Street" type="xs:string" />
<xs:element name="City" type="xs:string" />
<xs:element name="zip" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Any comments, suggestions, feedback, bugs, errors are highly appreciated at jag_chat@yahoo.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |