我有这个 XML 模式,但我不知道如何完成它以实现我所需要的。我在网上搜索了很多关于 unique 和 keyref 用法的信息,但我能找到的只是基本示例。
这是我的架构:
<xs:element name="access" type="myaccess" />
<xs:complexType name="myaccess">
<xs:sequence>
<xs:element name="user" type="myuser" minOccurs="0" maxOccurs="unbounded">
<xs:unique name="u_idunique">
<xs:selector xpath="user" />
<xs:field xpath="@id" />
</xs:unique>
</xs:element>
<xs:element name="authorization" type="myauthorization" minOccurs="0" maxOccurs="unbounded">
<!-- HERE I WANT A KEYREF TO id attribute of user element -->
<!-- HERE I WANT A KEYREF TO id attribute of building element OR door element -->
</xs:element>
<xs:element name="building" type="mybuilding" minOccurs="0" maxOccurs="unbounded" >
<!-- I DON'T KNOW HOW TO SPECIFY THAT id of building, id of door and id of gate are in the same scope -->
<xs:unique name="b_idunique">
<xs:selector xpath="building" />
<xs:field xpath="@id" />
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="myuser">
<xs:attribute name="id" type="my_id" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="phone" type="my_string_numeric" use="required" />
</xs:complexType>
<xs:complexType name="mybuilding">
<xs:sequence>
<xs:element name="door" type="mydoor" minOccurs="0" maxOccurs="unbounded">
<!-- I DON'T KNOW HOW TO SPECIFY THAT id of building, id of door and id of gate are in the same scope -->
<xs:unique name="d_idunique">
<xs:selector xpath="door" />
<xs:field xpath="@id" />
</xs:unique>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="my_id" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="country" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="mydoor">
<xs:sequence>
<xs:element name="gate" type="mygate" maxOccurs="unbounded">
<!-- I DON'T KNOW HOW TO SPECIFY THAT id of building, id of door and id of gate are in the same scope -->
<xs:unique name="g_idunique">
<xs:selector xpath="gate" />
<xs:field xpath="@id" />
</xs:unique>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="my_id" use="required" />
<xs:attribute name="address" type="xs:string" use="required" />
<xs:attribute name="status" type="mystatus" default="DISABLED" />
</xs:complexType>
<xs:complexType name="mygate">
<xs:attribute name="id" type="my_id" use="required" />
<xs:attribute name="type" type="mytype" use="required" />
<xs:attribute name="status" type="mystatus" default="DISABLED" />
</xs:complexType>
<xs:complexType name="myauthorization">
<xs:sequence>
<xs:element name="validityperiod" type="myvalidityperiod" />
</xs:sequence>
<xs:attribute name="idu" type="my_id" use="required" />
<xs:attribute name="idao" type="my_id" use="required" />
</xs:complexType>
<!-- OMITTED USELESS PART OF THE SCHEMA -->
</xs:schema>
我有两个问题:
- 我不知道如何指定建筑物的 id 字段,门的 id 字段和门的 id 字段在同一范围内,我不能有 2 个 id 等于(两个建筑物不能有相同的 id , 而且一扇门和一栋楼不能共享同一个id)
- 我不知道如何正确使用 keyref 元素。
- 我希望授权元素的 idu 字段是一个存在于用户元素之一中的 id(请参见下面的 [*])。
- 我希望授权元素的 idao 字段是存在于建筑元素之一或门元素之一中的 id。
[*] 我试着写这个,但它不起作用:
<xs:keyref name="useridkeyref" refer="u_idunique">
<xs:selector xpath="authorization" />
<xs:field xpath="@idu" />
</xs:keyref>
我知道这不是一个简短的问题,我提前感谢大家阅读它。我希望我能得到一些帮助。谢谢!