首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSD唯一元素和属性

XSD唯一元素和属性
EN

Stack Overflow用户
提问于 2015-12-30 16:02:11
回答 1查看 2.4K关注 0票数 2

各位!这是我关于堆栈溢出的第一个问题,尽管我经常在这个站点上阅读文章。

为了说明问题,我试图定义一个XML模式,如下所示:

代码语言:javascript
复制
<xs:element name="keyConfiguration">
<xs:complexType>
  <xs:sequence>
    <xs:element name="move">
      <xs:complexType>
        <xs:attribute name="N" type="keyCode"/>
        <xs:attribute name="NE" type="keyCode"/>
        <xs:attribute name="E" type="keyCode"/>
        <xs:attribute name="SE" type="keyCode"/>
        <xs:attribute name="S" type="keyCode"/>
        <xs:attribute name="SW" type="keyCode"/>
        <xs:attribute name="W" type="keyCode"/>
        <xs:attribute name="NW" type="keyCode"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="wait" type="keyCode"/>
    <xs:element name="select" type="keyCode"/>
  </xs:sequence>
</xs:complexType>
<xs:unique name="uniqueKeyCode">
  <xs:selector xpath="."/>
  <xs:field xpath="move/@*"/>
  <xs:field xpath="wait"/>
  <xs:field xpath="select"/>
</xs:unique>

keyCode是一个枚举类型,用于识别键盘按压,它接受xs:int的一个子集。

这样做的想法是,我不想验证将多个可能的操作映射到同一个键的XML文件,因此以下XML应该无效:

代码语言:javascript
复制
<keyConfiguration>
  <move N="101" NE="101" E="102" SE="99" S="98" SW="97" W="100" NW="103"/>
  <wait>101</wait>
  <select>101</select>
</keyConfiguration>

移动到北、东北等的属性和等待/选择操作的元素都会重复,不应该发生。移动方向的所有分配和其他操作的所有元素都应该是唯一的。

可悲的是,当我试图根据XSD验证给定的XML时,它是有效的!我认为唯一标记中的XPath已经损坏,但我不知道如何解决这个问题(我尝试了多个变体,包括<xs:field xpath="*/move/@*"/><xs:field xpath="*/wait"/>,但它仍然不起作用)。

提前感谢!

编辑:这是完整的模式定义,如果它有帮助的话:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="keyCode">
    <xs:restriction base="xs:int">
      <xs:enumeration value="10"/> <!-- Enter -->
      ...
      <xs:enumeration value="96"/> <!-- NumPad-0 -->
      <xs:enumeration value="97"/> <!-- NumPad-1 -->
      <xs:enumeration value="98"/> <!-- NumPad-2 -->
      <xs:enumeration value="99"/> <!-- NumPad-3 -->
      <xs:enumeration value="100"/> <!-- NumPad-4 -->
      <xs:enumeration value="101"/> <!-- NumPad-5 -->
      <xs:enumeration value="102"/> <!-- NumPad-6 -->
      <xs:enumeration value="103"/> <!-- NumPad-7 -->
      <xs:enumeration value="104"/> <!-- NumPad-8 -->
      <xs:enumeration value="105"/> <!-- NumPad-9 -->
      ...
      </xs:restriction>
  </xs:simpleType>

  <xs:element name="keyConfiguration">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="move">
          <xs:complexType>
            <xs:attribute name="N" type="keyCode"/>
            <xs:attribute name="NE" type="keyCode"/>
            <xs:attribute name="E" type="keyCode"/>
            <xs:attribute name="SE" type="keyCode"/>
            <xs:attribute name="S" type="keyCode"/>
            <xs:attribute name="SW" type="keyCode"/>
            <xs:attribute name="W" type="keyCode"/>
            <xs:attribute name="NW" type="keyCode"/>
          </xs:complexType>
        </xs:element>
        <xs:element name="wait" type="keyCode"/>
        <xs:element name="select" type="keyCode"/>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="uniqueKeyCode">
    <xs:selector xpath="."/>
      <xs:field xpath="*/move/@*"/>
      <xs:field xpath="*/wait"/>
      <xs:field xpath="*/select"/>
    </xs:unique>
  </xs:element>
</xs:schema>

也许它与命名空间有关?我试着用xpath示例查找web,但找不到任何可以帮助我识别问题的东西。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-01 13:03:32

我认为使用XSD1.0无法做到这一点。unique是这样的: xs:selector在所有的域中选择一组元素,值应该是唯一的。

因此,在选择器中,您应该选择每个属性、值和值,并在字段中选择这些节点的值(点字符".")。请记住,在XPath中,操作符x给出节点集之间的联合。理想情况下,您可以使用它来解决您的问题:

代码语言:javascript
复制
<xs:unique name="uniqueKeyCode">
    <xs:selector xpath="move/@* | wait | select"/>
    <xs:field xpath="."/>
</xs:unique>

但是,XSD不允许在xs:selector中选择属性。但我希望你明白,如果N、NE、W等是元素,而不是属性,则允许使用以下内容,它将起作用:

代码语言:javascript
复制
<xs:unique name="uniqueKeyCode">
    <xs:selector xpath="move/* | wait | select"/>
    <xs:field xpath="."/>
</xs:unique>

这将有效,因为您只选择xs:selector中的元素。

使用XSD1.1,这可以使用xs:assert来完成,这允许您使用xpath (选择器、字段、唯一的等等,等等只允许您使用受限的XPath子集)。将解决此问题的示例断言:

代码语言:javascript
复制
<xs:assert test="count(distinct-values(move/@* | wait | select)) = count(move/@* | wait | select)"/>

此外,请记住,使用范围(从10到105)和使用联合(从10到50 +从60到105)来定义keyCode类型更容易,而不是使用xs:枚举。

连续值示例:

代码语言:javascript
复制
<xs:simpleType name="keyCode">
    <xs:restriction base="xs:int">
        <xs:minInclusive value="10"/>
        <xs:maxInclusive value="105"/>
    </xs:restriction>
</xs:simpleType>

非连续值的示例:

代码语言:javascript
复制
<xs:simpleType name="keyCode">
    <xs:union>
        <xs:simpleType>
            <xs:restriction base="xs:int">
                <xs:minInclusive value="10"/>
                <xs:maxInclusive value="50"/>
            </xs:restriction>
        </xs:simpleType>

        <xs:simpleType>
            <xs:restriction base="xs:int">
                <xs:minInclusive value="60"/>
                <xs:maxInclusive value="105"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:union>
</xs:simpleType>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34532872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档