XML模式中的关键约束无法正常工作/应用

时间:2016-03-28 14:00:31

标签: xml xsd xsd-validation

所以我在这里有一个XML模式:

<?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="sid" 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>
                    <!-- key reference to employee ID for use as a foreign key -->
                    <xs:key name="employeeID">
                        <xs:selector xpath="employee" />
                        <xs:field xpath="@eid" />
                    </xs:key>
                </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>
                                <!-- key reference to employee ID for use as a foreign key -->
                                <xs:key name="customerID">
                                    <xs:selector xpath="customer" />
                                    <xs:field xpath="@cid" />
                                </xs:key>
                            </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" type="xs:decimal" />
                                        <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:keyref refer="employeeID" name="FK_managerID">
            <xs:selector xpath="branches/branch/manager" />
            <xs:field xpath="@mid" />
        </xs:keyref>
        <xs:keyref refer="employeeID" name="FK_supervisorID">
            <xs:selector xpath="employees/employee/supervisor" />
            <xs:field xpath="@sid" />
        </xs:keyref>
        <xs:keyref refer="customerID" name="FK_customerIDForPackage">
            <xs:selector xpath="packages/package" />
            <xs:field xpath="@cid" />
        </xs:keyref>
    </xs:element>
</xs:schema>

现在,密钥如下:  - 员工ID   - customerID

应该清楚这些应该在哪里引导,即employeeID是分支的managerID以及员工的supervisorID。

但它没有用!

这是我的文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="couriersystem.xsl"?>
<couriersystem title="Courier System"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="couriersystem.xsd">
    <branches>
        <branch bid="1">
            <name>Headquarters</name>
            <address>
                58, Main Street, Edinburgh, R79 2LR
            </address>
            <manager mid="1" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="none" />
            </deliverymethods>
        </branch>
    </branches>
    <employees>
        <employee eid="1">
            <nin>AZ123518D</nin>
            <firstname>Peter</firstname>
            <lastname>Smith</lastname>
            <gender>Male</gender>
            <dob>1994-02-11</dob>
            <email>ps11@gmail.com</email>
            <address>
                119, London Street, Nidrie, F57 8NE
            </address>
            <tel>07005748900</tel>
            <salary>30526</salary>
            <empbranch bid="1" />
            <supervisor sid="1" />
        </employee>
    </employees>
    <customers>
        <customer cid="1">
            <firstname>Sam</firstname>
            <lastname>Mitchell</lastname>
            <gender>Male</gender>
            <dob>1991-12-12</dob>
            <email>sammitch@hotmail.co.uk</email>
            <address>
                161, South Road, Nidrie, W79 8WG
            </address>
            <tel>07811119542</tel>
            <cbranch bid="1" />
        </customer>
    </customers>
    <packages>
        <package pid="1" cid="1">
            <name>ACER Aspire F5-571 Laptop</name>
            <weight>3.5</weight>
            <price>30</price>
            <category></category>
        </package>
    </packages>
</couriersystem>

1 个答案:

答案 0 :(得分:1)

customerID密钥应在customers元素中声明(不在customer元素中)。

<xs:element name="customers">
  <xs:complexType>
    ...
  </xs:complexType>
  <!-- key reference to employee ID for use as a foreign key -->
  <xs:key name="customerID">
      <xs:selector xpath="customer" />
      <xs:field xpath="@cid" />
  </xs:key>
</xs:element>