1

I am using XSD for XML validation. I want to add unique values constraint for the input elements.

I have XML format like this:

<?xml version="1.0" encoding="UTF-8"?>
<test>
  <definitions>
    <input>Page</input>
  </definitions>
  <definitions>
    <input>Page</input>
  </definitions>
</test>

My XSD:

<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="test">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="definitions" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="input"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I want to know how xs:unique should be placed.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
priyanka
  • 305
  • 1
  • 3
  • 18
  • What do you mean with "_how unique tag be placed_"? Furthermore, I don't see any attribute in here. – potame Mar 12 '15 at 07:43
  • I am asking how tag to be placed for input tags, as i am not aware of this – priyanka Mar 12 '15 at 08:55
  • It is still not clear on what you want to be unique. – potame Mar 12 '15 at 09:36
  • I am sorry, I would reframe my question, Iwant value of the tag to be unqiue. As in my xml, "Page" value is duplicated. SO I want to add unique constraint for – priyanka Mar 12 '15 at 10:02

2 Answers2

2

To place the xs:unique element:

  1. Identify the scope of uniqueness (test) for the elements of xs:unique/@selector (definitions).
  2. Place the xs:unique element
    • within the declaration of test, and
    • after the xs:complexType for test

See HERE in the XSD below:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">
  <xs:element name="test">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="definitions"
                    maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="input"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>

    <!-- HERE -->
    <xs:unique name="definitions-input-unique">
      <xs:selector xpath="definitions"/>
      <xs:field xpath="input"/>
    </xs:unique>

  </xs:element>
</xs:schema>

Then this invalid XML

<?xml version="1.0" encoding="UTF-8"?>
<test>
  <definitions>
    <input>Page</input>
  </definitions>
  <definitions>
    <input>Page</input>
  </definitions>
</test>

will receive an error message such as the following:

[Error] try.xml:7:24: cvc-identity-constraint.4.1: Duplicate unique value [Page] declared for identity constraint "definitions-input-unique" of element "test".

kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

Just put it at the top level like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:element name="test">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="definitions" maxOccurs="unbounded" minOccurs="0">
     <xs:complexType>
      <xs:sequence>
       <xs:element type="xs:string" name="input"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
  <xs:unique name="input-values">
   <xs:selector xpath="./definitions"/>
   <xs:field xpath="input"/>
  </xs:unique>
 </xs:element>
</xs:schema>