4

我有这个 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 元素。
    1. 我希望授权元素的 idu 字段是一个存在于用户元素之一中的 id(请参见下面的 [*])。
    2. 我希望授权元素的 idao 字段是存在于建筑元素之一或门元素之一中的 id。

[*] 我试着写这个,但它不起作用:

<xs:keyref name="useridkeyref" refer="u_idunique">
    <xs:selector xpath="authorization" />   
    <xs:field xpath="@idu" />
</xs:keyref>

我知道这不是一个简短的问题,我提前感谢大家阅读它。我希望我能得到一些帮助。谢谢!

4

2 回答 2

4

唯一约束和键是在element级别范围内限定的 - 您需要将约束不是放在每个单独的元素内,而是放在作为access所有元素的共同祖先的元素内。

<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>
于 2013-12-18T16:10:55.043 回答
0

就我而言,我还必须为选择器声明和使用显式命名空间。使用解决方案Ian Roberts并调整这些属性:

<xs:schema ...
        xmlns:mc="http://mycompany.com"
...
>
...    
    <xs:selector xpath="mc:user" />      
  ...      
    <xs:selector xpath="mc:authorization" />
  ...
 </xs:element>

另请参阅此处https://stackoverflow.com/a/22353694/1909531

于 2018-06-04T14:37:33.157 回答