Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique constraint in XML Schema

Let's say I have following XML file:

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

and an XML schema fragment:

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

What I want is to make a constraint that will not allow two identical author values, but the one above doesn't work that way. What am I doing wrong?

like image 709
BartoszCichecki Avatar asked Oct 10 '11 19:10

BartoszCichecki


People also ask

How to make an element unique in XSD?

You can create a Unique constraint within an XSD using the <xs:unique> element. The selector and field pair specify the data that must be unique, the XPath expressions are relative to the parent element (Directory).

What are the constraints can apply for XML schema?

In an XML Schema definition language (XSD) schema, you can specify constraints (unique, key, and keyref constraints) and relationships (using the msdata:Relationship annotation). This topic explains how the constraints and relationships specified in an XML Schema are interpreted to generate the DataSet.

What is minOccurs in XML?

An array with a varying number of elements is represented in the XML schema by using the minOccurs and maxOccurs attributes on the element declaration: The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer.

What is complexType in XML?

An element of type complexType can contain text, elements, and attributes. An element of type complexType is parent to all the elements and attributes contained within it. An any element in an XSD specifies that any well-formed XML is allowed in its place in XML instance.


2 Answers

The selector XPath selects the nodes that must be unique (in that case, it should select the author nodes).

The field XPath selects what "makes them unique" (in that case, using . will cause their typed value, in that case the text between the tags, treated as a string, to be used).

The document

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

should be valid against the following schema:

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

while this one should not:

<?xml version="1.0" encoding="UTF-8"?>
<authors>
  <author>a1</author>
  <author>a1</author>
  <lastmodified>2010-01-01</lastmodified>
</authors>
like image 57
Ghislain Fourny Avatar answered Oct 16 '22 11:10

Ghislain Fourny


You could use type="xs:ID" on the author element. There is also type IDREF for referring to an ID.

like image 1
Dabbler Avatar answered Oct 16 '22 11:10

Dabbler