将外键约束添加到XML架构属性

时间:2016-03-25 16:04:30

标签: xml xsd

所以我有以下架构:

<?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>

我想知道如何针对某些数据添加外键限制/引用。大多数情况下,这些数据都是属性,例如第20行的mid应该引用员工的身份eid。总公司hid应该引用分支机构的身份bid

知道我怎么能这样做吗?我已经在网上查找了有关xs:keyxs:keyref之类的人员代码的其他建议,但我不知道在哪里或如何使用这些建议!

谢谢!

1 个答案:

答案 0 :(得分:2)

要在经理和员工之间建立关系,您应该这样做:

使用@eid元素中的属性employees作为键:

              <xs:element name="employees">
                    <xs:complexType>
                      ...    
                    </xs:complexType>
                    <xs:key name="empKey">
                        <xs:selector xpath="employee"/>
                        <xs:field xpath="@eid"/>
                    </xs:key>
              </xs:element>

定义密钥后,我们可以通过定义keyref来引用此密钥。

<xs:element name="couriersystem">
        <xs:complexType>
           .....
        </xs:complexType>
        <xs:keyref refer="empKey" name="FK_emp_manager">
            <xs:selector xpath="branches/branch/manager"/>
            <xs:field xpath="@mid"/>
        </xs:keyref>
    </xs:element>

它表示元素@mid中的属性manager用作keyref,它引用empKey中的键employees。因此,属性@mid的值应与关联的@eid元素中的属性employee的值相同。

注意: xs:keyref需要与匹配的xs:key或其祖先之一在同一元素中。