I have this XML schema but I don't know how to complete it in order to achieve what I need. I searched a lot online about unique and keyref usage, but all I can find are basic examples.
我有这个XML模式,但我不知道如何完成它以实现我需要的。我在网上搜索了很多关于unique和keyref的用法,但我能找到的只是基本的例子。
This is my schema:
这是我的架构:
<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>
I have two problems:
我有两个问题:
- I don't know how to specify that the id field of building, the id field of door and the id field of gate are in the same scope and I can't have 2 id equals (two building can't have the same id, but also a door and a building can't share the same id)
- I don't know how to use correctly the keyref element.
- I'd like that idu filed of authorization element is an id that is present in one of the user elements (see [*] below).
- I'd like that the idao field of authorization element is an id that is present in one of the building elements OR one of the door elements.
我希望授权元素的idu字段是一个用户元素中的id(参见下面的[*])。
我希望授权元素的idao字段是一个id,它存在于一个建筑元素或其中一个门元素中。
我不知道如何指定构建的id字段,门的id字段和门的id字段在同一范围内,我不能有2个id等于(两个建筑物不能具有相同的id ,还有一扇门和一幢建筑物不能共用同一个id)
我不知道如何正确使用keyref元素。我希望授权元素的idu字段是一个用户元素中的id(参见下面的[*])。我希望授权元素的idao字段是一个id,它存在于一个建筑元素或其中一个门元素中。
[*] I tried to write this, but it's not working:
[*]我试着写这个,但它不起作用:
<xs:keyref name="useridkeyref" refer="u_idunique">
<xs:selector xpath="authorization" />
<xs:field xpath="@idu" />
</xs:keyref>
I know this is not a short question and I thanks everyone in advance for reading it. I hope I can get some help. Thank you!
我知道这不是一个简短的问题,我提前感谢所有人阅读它。我希望我能得到一些帮助。谢谢!
2 个解决方案
#1
4
Unique constraints and keys are scoped at the element
level - you need to put the constraint not inside each individual element, but inside the access
element that is a common ancestor of them all.
唯一约束和键在元素级别作用域 - 您需要将约束放在每个单独元素内部,而不是放在它们共同祖先的访问元素内部。
<xs:element name="access" type="myaccess">
<xs:key name="user_id">
<xs:selector xpath="user" />
<xs:field xpath="@id" />
</xs:key>
<xs:key name="access_id">
<xs:selector xpath="building | building/door | building/door/gate" />
<xs:field xpath="@id" />
</xs:key>
<xs:keyref name="user_ref" refer="user_id">
<xs:selector xpath="authorization" />
<xs:field xpath="@idu" />
</xs:keyref>
<xs:keyref name="access_ref" refer="access_id">
<xs:selector xpath="authorization" />
<xs:field xpath="@idao" />
</xs:keyref>
</xs:element>
#2
0
In my case I also had to declare and use an explicit namespace for the selectors. Use the solution from Ian Roberts
and adapt these attributes:
在我的情况下,我还必须为选择器声明并使用显式命名空间。使用Ian Roberts的解决方案并调整这些属性:
<xs:schema ...
xmlns:mc="http://mycompany.com"
...
>
...
<xs:selector xpath="mc:user" />
...
<xs:selector xpath="mc:authorization" />
...
</xs:element>
See also here https://*.com/a/22353694/1909531
另请参见https://*.com/a/22353694/1909531
#1
4
Unique constraints and keys are scoped at the element
level - you need to put the constraint not inside each individual element, but inside the access
element that is a common ancestor of them all.
唯一约束和键在元素级别作用域 - 您需要将约束放在每个单独元素内部,而不是放在它们共同祖先的访问元素内部。
<xs:element name="access" type="myaccess">
<xs:key name="user_id">
<xs:selector xpath="user" />
<xs:field xpath="@id" />
</xs:key>
<xs:key name="access_id">
<xs:selector xpath="building | building/door | building/door/gate" />
<xs:field xpath="@id" />
</xs:key>
<xs:keyref name="user_ref" refer="user_id">
<xs:selector xpath="authorization" />
<xs:field xpath="@idu" />
</xs:keyref>
<xs:keyref name="access_ref" refer="access_id">
<xs:selector xpath="authorization" />
<xs:field xpath="@idao" />
</xs:keyref>
</xs:element>
#2
0
In my case I also had to declare and use an explicit namespace for the selectors. Use the solution from Ian Roberts
and adapt these attributes:
在我的情况下,我还必须为选择器声明并使用显式命名空间。使用Ian Roberts的解决方案并调整这些属性:
<xs:schema ...
xmlns:mc="http://mycompany.com"
...
>
...
<xs:selector xpath="mc:user" />
...
<xs:selector xpath="mc:authorization" />
...
</xs:element>
See also here https://*.com/a/22353694/1909531
另请参见https://*.com/a/22353694/1909531