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.

4
  • What do you mean with "how unique tag be placed"? Furthermore, I don't see any attribute in here.
    – potame
    Commented Mar 12, 2015 at 7:43
  • I am asking how <xs:unique></xsunique> tag to be placed for input tags, as i am not aware of this
    – priyanka
    Commented Mar 12, 2015 at 8:55
  • It is still not clear on what you want to be unique.
    – potame
    Commented Mar 12, 2015 at 9:36
  • I am sorry, I would reframe my question, Iwant value of the <input> tag to be unqiue. As in my xml, "Page" value is duplicated. SO I want to add unique constraint for <input>
    – priyanka
    Commented Mar 12, 2015 at 10:02

2 Answers 2

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

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>

Not the answer you're looking for? Browse other questions tagged or ask your own question.