Friday, 25 March 2016

XML : Adding foreign key restraints to XML schema attributes

So i have the following schema:

  <?xml version="1.0" encoding="UTF-8"?>  <xs:schema      xmlns:xs="http://www.w3.org/2001/XMLSchema"      elementFormDefault="qualified">      <xs:element name="couriersystem">          <xs:complexType>              <xs:sequence>                  <!-- branches -->                  <xs:element name="branches">                      <xs:complexType>                          <xs:sequence>                              <xs:element name="branch" maxOccurs="unbounded">                                  <xs:complexType>                                      <xs:sequence>                                          <xs:element name="name" type="xs:string" />                                          <xs:element name="address" type="xs:string" />                                          <!-- foreign key to employee (manager) -->                                          <xs:element name="manager">                                              <xs:complexType>                                                  <xs:attribute name="mid" type="xs:positiveInteger" use="required" />                                              </xs:complexType>                                          </xs:element>                                          <!-- foreign key to branch (head office) -->                                          <xs:element name="headoffice">                                              <xs:complexType>                                                  <xs:attribute name="hid" type="xs:positiveInteger" use="required" />                                              </xs:complexType>                                          </xs:element>                                          <!-- delivery methods -->                                          <xs:element name="deliverymethods">                                              <xs:complexType>                                                  <xs:sequence>                                                      <xs:element name="method" maxOccurs="unbounded">                                                          <xs:complexType>                                                              <xs:attribute name="name" type="xs:string" use="required" />                                                          </xs:complexType>                                                      </xs:element>                                                  </xs:sequence>                                              </xs:complexType>                                          </xs:element>                                      </xs:sequence>                                      <xs:attribute name="bid" type="xs:positiveInteger" use="required" />                                  </xs:complexType>                              </xs:element>                          </xs:sequence>                      </xs:complexType>                  </xs:element>                  <!-- employees -->                  <xs:element name="employees">                      <xs:complexType>                          <xs:sequence>                              <xs:element name="employee" maxOccurs="unbounded">                                  <xs:complexType>                                      <xs:sequence>                                          <xs:element name="nin">                                              <xs:simpleType>                                                  <xs:restriction base="xs:string">                                                      <xs:pattern value="^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-DFM]{0,1}$" />                                                  </xs:restriction>                                              </xs:simpleType>                                          </xs:element>                                          <xs:element name="firstname" type="xs:string" />                                          <xs:element name="lastname" type="xs:string" />                                          <xs:element name="gender">                                              <xs:simpleType>                                                  <xs:restriction base="xs:string">                                                      <xs:pattern value="Male|Female" />                                                  </xs:restriction>                                              </xs:simpleType>                                          </xs:element>                                          <xs:element name="dob" type="xs:date" />                                          <xs:element name="email">                                              <xs:simpleType>                                                  <xs:restriction base="xs:string">                                                      <xs:pattern value="[^@]+@[^\.]+\..+" />                                                  </xs:restriction>                                              </xs:simpleType>                                          </xs:element>                                          <xs:element name="address" type="xs:string" />                                          <xs:element name="tel">                                              <xs:simpleType>                                                  <xs:restriction base="xs:string">                                                      <!--                                                          Accepts the following:                                                          07222 555555 | (07222) 555555 | +44 7222 555 555                                                      -->                                                      <xs:pattern value="^(07\d{8,12}|447\d{7,11})$" />                                                  </xs:restriction>                                              </xs:simpleType>                                          </xs:element>                                          <xs:element name="salary" type="xs:positiveInteger" />                                          <!-- foreign key to branch (employee's branch) -->                                          <xs:element name="empbranch">                                              <xs:complexType>                                                  <xs:attribute name="bid" type="xs:positiveInteger" use="required" />                                              </xs:complexType>                                          </xs:element>                                          <!-- foreign key to employee (supervisor) -->                                          <xs:element name="supervisor">                                              <xs:complexType>                                                  <xs:attribute name="eid" type="xs:positiveInteger" use="required" />                                              </xs:complexType>                                          </xs:element>                                      </xs:sequence>                                      <xs:attribute name="eid" type="xs:positiveInteger" use="required" />                                  </xs:complexType>                              </xs:element>                          </xs:sequence>                      </xs:complexType>                  </xs:element>                  <!-- customers -->                  <xs:element name="customers">                      <xs:complexType>                          <xs:sequence>                              <xs:element name="customer" maxOccurs="unbounded">                                  <xs:complexType>                                      <xs:sequence>                                          <xs:element name="firstname" type="xs:string" />                                          <xs:element name="lastname" type="xs:string" />                                          <xs:element name="gender">                                              <xs:simpleType>                                                  <xs:restriction base="xs:string">                                                      <xs:pattern value="Male|Female" />                                                  </xs:restriction>                                              </xs:simpleType>                                          </xs:element>                                          <xs:element name="dob" type="xs:date" />                                          <xs:element name="email">                                              <xs:simpleType>                                                  <xs:restriction base="xs:string">                                                      <xs:pattern value="[^@]+@[^\.]+\..+" />                                                  </xs:restriction>                                              </xs:simpleType>                                          </xs:element>                                          <xs:element name="address" type="xs:string" />                                          <xs:element name="tel">                                              <xs:simpleType>                                                  <xs:restriction base="xs:string">                                                      <!--                                                          Accepts the following:                                                          07222 555555 | (07222) 555555 | +44 7222 555 555                                                      -->                                                      <xs:pattern value="^(07\d{8,12}|447\d{7,11})$" />                                                  </xs:restriction>                                              </xs:simpleType>                                          </xs:element>                                          <!-- foreign key to branch (customer's branch id) -->                                          <xs:element name="cbranch">                                              <xs:complexType>                                                  <xs:attribute name="bid" type="xs:positiveInteger" use="required" />                                              </xs:complexType>                                          </xs:element>                                      </xs:sequence>                                      <xs:attribute name="cid" type="xs:positiveInteger" use="required" />                                  </xs:complexType>                              </xs:element>                          </xs:sequence>                      </xs:complexType>                  </xs:element>                  <!-- packages -->                  <xs:element name="packages">                      <xs:complexType>                          <xs:sequence>                              <xs:element name="package" maxOccurs="unbounded">                                  <xs:complexType>                                      <xs:sequence>                                          <xs:element name="name" type="xs:string" />                                          <xs:element name="weight">                                              <xs:simpleType>                                                  <xs:restriction base="xs:string">                                                      <xs:pattern value="kg|lbs" />                                                  </xs:restriction>                                              </xs:simpleType>                                          </xs:element>                                          <xs:element name="price" type="xs:positiveInteger" />                                          <xs:element name="category" type="xs:string" />                                      </xs:sequence>                                      <xs:attribute name="pid" type="xs:positiveInteger" use="required" />                                      <!-- link to customer id -->                                      <xs:attribute name="cid" type="xs:positiveInteger" use="required" />                                  </xs:complexType>                              </xs:element>                          </xs:sequence>                      </xs:complexType>                  </xs:element>              </xs:sequence>              <xs:attribute name="title" type="xs:string" use="required" />          </xs:complexType>      </xs:element>  </xs:schema>    

And I was wondering how I would go about adding foreign key restraints/references against some of the data. Mostly these data are attributes such as the mid on line 20 should refer to an employee's id eid. And a head office hid should refer to a branch's id bid.

Any idea how I can do this? I've looked up other suggestions online for peoples code for things like xs:key and xs:keyref but I don't know where or how I would use these!

Thanks!

No comments:

Post a Comment