繁体   English   中英

XML Schema中的唯一约束

[英]Unique constraint in XML Schema

假设我有以下XML文件:

<authors>
   <author>a1</author>
   <author>a2</author>
   <lastmodified>2010</lastmodified>
</authors>

和XML模式片段:

<xs:element name="authors" maxOccurs="1">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="author" maxOccurs="unbounded" type="xs:string"> </xs:element>
      <xs:element name="lastmodified" type="xs:date" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:unique name="uniqueAuthor">
     <xs:selector xpath="."/>
     <xs:field xpath="author"/>
  </xs:unique>
</xs:element>

我想要的是制作一个不允许两个相同的作者值的约束,但上面的那个不起作用。 我究竟做错了什么?

selector XPath选择必须唯一的节点(在这种情况下,它应该选择作者节点)。

field XPath选择“使它们唯一”的内容(在这种情况下,使用.将导致它们的类型值,在这种情况下,标记之间的文本被视为字符串,被使用)。

该文件

<?xml version="1.0" encoding="UTF-8"?>
<authors>
  <author>a1</author>
  <author>a2</author>
  <lastmodified>2010-01-01</lastmodified>
</authors>

应该对以下架构有效:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="authors">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="author" maxOccurs="unbounded" type="xs:string"/>
        <xs:element name="lastmodified" type="xs:date" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="uniqueAuthor">
      <xs:selector xpath="author"/>
      <xs:field xpath="."/>
    </xs:unique>
  </xs:element>
</xs:schema>

而这个不应该:

<?xml version="1.0" encoding="UTF-8"?>
<authors>
  <author>a1</author>
  <author>a1</author>
  <lastmodified>2010-01-01</lastmodified>
</authors>

您可以在author元素上使用type =“xs:ID”。 还有类型IDREF用于引用ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM