One-One, One-Many and Many-Many Relations in XML Schema - Implementing a “One to One” relation in XML Schema: a sample XML document
(Page 3 of 6 )
In the previous section, I defined an XML Schema to enforce a one to one relation in between two element hierarchies. The following would be an XML document sample for the previously defined XML Schema.
<?xml version="1.0" encoding="utf-8"?>
<Organization>
<Employee>
<PersonalDetails>
<Empno>1001</Empno>
<FirstName>Jagadish</FirstName>
<LastName>Chatarji</LastName>
<DOB>1978-06-25</DOB>
</PersonalDetails>
<AddressDetails>
<Empno>1001</Empno>
<Street>13-2-26, Nallam vari thota,
Gunupudi</Street>
<City>Bhimavaram</City>
<ZIP>534201</ZIP>
</AddressDetails>
</Employee>
<Employee>
<PersonalDetails>
<Empno>1002</Empno>
<FirstName>Winner</FirstName>
<LastName></LastName>
<DOB>1985-09-08</DOB>
</PersonalDetails>
<AddressDetails>
<Empno>1002</Empno>
<Street>some address</Street>
<City>some city</City>
<ZIP>some zip</ZIP>
</AddressDetails>
</Employee>
</Organization>
The above XML document is error free, as the values in the “Empno” element of “AddressDetails” exist in the “Empno” element of “PersonalDetails.” Try to change “1002” (in “AddressDetails”) to “1003” (which does not exist in “PersonalDetails”). The XML document becomes invalid during parsing!
There is another issue to remember about the “Employee” element. I just meant it to separate the employee details only for the sake of explanation. It would be more practical if we remove “Employee” and directly have “PersonalDetails” and “AddressDetails” under the root element “Organization.”
Next: Implementing a “One to Many” relation in XML Schema: the schema >>
More XML Articles
More By Jagadish Chaterjee