One-One, One-Many and Many-Many Relations in XML Schema - Implementing a “One to One” relation in XML Schema: schema explained (Page 2 of 6 )
This section explains the schema introduced in the previous section. Instead of explaining it all at once, let me explain it part by part. Let us consider the following fragment:
<xs:complexType name="typePersonal">
<xs:sequence>
<xs:element name="Empno" type="xs:int" />
<xs:element name="FirstName" type="xs:string" />
<xs:element name="LastName" type="xs:string" />
<xs:element name="DOB" type="xs:date" />
</xs:sequence>
</xs:complexType>
The above code fragment defines a new user-defined complex type named “typePersonal.” It internally contains four elements, namely “Empno,” “FirstName,” “LastName” and “DOB.” There exists another code fragment very similar to the one above:
<xs:complexType name="typeAddress">
<xs:sequence>
<xs:element name="Empno" type="xs:int" />
<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>
The above code fragment defines a new user-defined complex type named “typeAddress.” It internally contains four elements, namely “Empno,” “Street,” “City” and “ZIP.” The above two are simply “data types.” We need to declare elements based on the above user-defined data types. Let us consider the following code fragment:
Within the above code fragment, you can observe that I defined a new element called “Employee” (which typically handles both of the previously defined data types). Within this element, I defined two more elements, “PersonalDetails” and “AddressDetails” based on the user-defined complex types explained previously. What about our primary keys? They are defined as follows:
<xs:key name="PK_Employee">
<xs:selector xpath=".//Employee" />
<xs:field xpath="PersonalDetails/Empno" />
</xs:key>
<xs:key name="PK_Employee_Address">
<xs:selector xpath=".//AddressDetails" />
<xs:field xpath="Empno" />
</xs:key>
The following code fragment enforces the one to one relation between “PersonalDetails” and “AddressDetails:”