user1806971
user1806971

Reputation: 21

XSD Unique constraint is not working

the XSD unique constraint does not seem to be working for me. I have the following 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>

and when I validate the XML below in both Eclipse and an online validator, no errors are returned:

 <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>

It looks simple and there's examples out there that I have followed but this is driving me mad! :-)

Any ideas?

Upvotes: 1

Views: 1297

Answers (2)

MG Developer
MG Developer

Reputation: 918

Alternatively xs:ID can be used instead of xs:string. Also see xs:IDREF and xs:IDREFS.

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

Upvotes: 0

user2039322
user2039322

Reputation: 63

In the XPath expression in the selector element, the prefix "tns" has to be used.

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

Upvotes: 4

Related Questions