向 XML 模式属性添加外键约束
Adding foreign key restraints to XML schema attributes
所以我有以下架构:
<?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>
我想知道如何针对某些数据添加外键 restraints/references。大多数这些数据是属性,例如第 20 行的 mid
应该引用员工的 ID eid
。总公司 hid
应该引用分公司的 id bid
.
知道我该怎么做吗?我已经在网上查找了其他关于 xs:key
和 xs:keyref
之类的代码的建议,但我不知道在哪里或如何使用这些!
谢谢!
要在经理和员工之间建立关系,您应该这样做:
使用 employees
元素中的属性 @eid
作为键:
<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>
它表示元素manager
中的属性@mid
用作keyref,它引用employees
中的键empKey
。因此属性 @mid
的值应该与关联的 employee
元素中属性 @eid
的值相同。
注:
xs:keyref
需要与匹配的 xs:key
或其祖先之一位于同一元素中。
所以我有以下架构:
<?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>
我想知道如何针对某些数据添加外键 restraints/references。大多数这些数据是属性,例如第 20 行的 mid
应该引用员工的 ID eid
。总公司 hid
应该引用分公司的 id bid
.
知道我该怎么做吗?我已经在网上查找了其他关于 xs:key
和 xs:keyref
之类的代码的建议,但我不知道在哪里或如何使用这些!
谢谢!
要在经理和员工之间建立关系,您应该这样做:
使用 employees
元素中的属性 @eid
作为键:
<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>
它表示元素manager
中的属性@mid
用作keyref,它引用employees
中的键empKey
。因此属性 @mid
的值应该与关联的 employee
元素中属性 @eid
的值相同。
注:
xs:keyref
需要与匹配的 xs:key
或其祖先之一位于同一元素中。