【问题标题】:XSD Unique constraint is not workingXSD 唯一约束不起作用
【发布时间】:2013-01-18 23:23:25
【问题描述】:

XSD 唯一约束似乎对我不起作用。我有以下 XSD:

<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://beardedhen.com/form" 
    xmlns:tns="http://beardedhen.com/form" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

<xs:complexType name="OptionListType">
  <xs:sequence>
    <xs:element name="option" minOccurs="1" maxOccurs="unbounded">
      <xs:complexType>
     <xs:attribute name="label" type="xs:string" use="required"/> 
     <xs:attribute name="value" type="xs:string" use="required"/> 
  </xs:complexType>
    </xs:element>
  </xs:sequence>
  <xs:attribute name="name" type="xs:string" use="required"/> 
</xs:complexType>

<xs:element name="OptionList" type="tns:OptionListType">
  <xs:unique name="uniqueOptionListLabel">
<xs:selector xpath="option"/>
    <xs:field xpath="@label"/>
 </xs:unique>
</xs:element>

 </schema>

当我在 Eclipse 和在线验证器中验证下面的 XML 时,没有返回错误:

 <OptionList  xmlns="http://beardedhen.com/form" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    name="statusLevels">
    <option label="Critical" value="0"/>
    <option label="Warning" value="1"/>
    <option label="Warning" value="1"/>
    <option label="Warning" value="1"/>
    <option label="Good" value="4"/>
</OptionList>

它看起来很简单,并且有一些我遵循的例子,但这让我发疯! :-)

有什么想法吗?

【问题讨论】:

    标签: eclipse xsd unique-constraint xsd-validation


    【解决方案1】:

    也可以使用 xs:ID 代替 xs:string。另请参阅 xs:IDREF 和 xs:IDREFS。

    http://books.xmlschemata.org/relaxng/ch19-77151.html

    【讨论】:

      【解决方案2】:

      在选择器元素的 XPath 表达式中,必须使用前缀“tns”。

      <xs:selector xpath="tns:option"/>
      

      【讨论】:

        猜你喜欢
        • 2017-03-26
        • 1970-01-01
        • 2017-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多