Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsd unique constraint not working

Tags:

xml

xsd

I have a root Inserts tag, a sequence of Inserts tags, each with a "name" attribute.

I cannot get online validator to find out that there are duplicate "name" values.

We've been struggling for... days. Thanks for finding out.

XSD:

   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.osames.org/osamesorm"
  targetNamespace="http://www.osames.org/osamesorm" elementFormDefault="qualified">

  <xs:element name="Inserts">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Insert" maxOccurs="unbounded">
              <xs:complexType>
               <xs:simpleContent>
                  <xs:extension base="xs:string">
                    <xs:attribute name="name" type="xs:string" use="required"/>
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
        <xs:unique name="unique-isbn">
          <xs:selector xpath="Inserts/Insert"/>
          <xs:field xpath="@name"/>
        </xs:unique>
      </xs:element>

</xs:schema>

XML :

<?xml version="1.0" encoding="UTF-8"?>
<Inserts xmlns="http://www.osames.org/osamesorm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osames.org/osamesorm ./xml_schemas/verysimple.xsd">
<Insert name="BaseInsert">INSERT INTO {0} ({1}) values ({2});</Insert>
<Insert name="BaseInsert">INSERT INTO {0} ({1}) values ({2});</Insert>
</Inserts>
like image 446
barbara.post Avatar asked Mar 12 '14 13:03

barbara.post


People also ask

How to add a unique ID constraint to an XSD?

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

How do I add a unique constraint to an XML Schema?

You can specify a unique constraint on a combination of elements or attributes in the XML Schema. The following example demonstrates how to specify that a combination of CustomerID and CompanyName values must be unique for all Customers in any instance, by adding another xs:field element in the schema.

What is XSD unique in HTML?

<xsd:unique> Element. Specifies that an attribute or element value (or a combination of attribute or element values) must be unique within the specified scope. The value must be unique or nil. Attributes. The ID of this element. The id value must be of type ID and be unique within the document containing this element.

How to make a unique name for a user in XSD?

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


1 Answers

There are two problems in your schema:

The first one is that your selector XPath is incorrect, based on the location where you define it. The <xs:unique> element is within an <Inserts> element, yet your XPath reads Inserts/Insert, meaning that within that <Inserts> element, another <Inserts> element is expected, and only therein an <Insert> element.

The <xs:unique> constraint is, however, already within the <Inserts> element, which is why you just need to find the <Insert> element:

<xs:unique name="unique-isbn">
  <xs:selector xpath="Insert"/>
  <xs:field xpath="@name"/>
</xs:unique>

The second issue is that XPath has no notion of the default namespace defined in Xml with the xmlns attribute. The Insert element you refer to in your XPath is not the Insert element from your XSD's default namespace, but an Insert element without a namespace URI.

To deal with that, add a namespace prefix for your namespace (as an alternative to the default namespace) to your XSD file:

  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.osames.org/osamesorm" targetNamespace="http://www.osames.org/osamesorm" xmlns:o="http://www.osames.org/osamesorm" elementFormDefault="qualified">

Then, use that namespace prefix in your XPath:

<xs:unique name="unique-isbn">
  <xs:selector xpath="o:Insert"/>
  <xs:field xpath="@name"/>
</xs:unique>

With these changes, this validator outputs

There is a duplicate key sequence 'BaseInsert' for the 'http://www.osames.org/osamesorm:unique-isbn' key or unique identity constraint. Line: 1 Column:357

like image 99
O. R. Mapper Avatar answered Oct 20 '22 05:10

O. R. Mapper