[oXygen-user] Using xs:unique

Florent Georges lists at fgeorges.org
Thu Nov 29 12:00:54 CST 2012


Olumide wrote:

  Hi,

> I'd appreciate help figuring out why the schema below validates the 
> following xml file even though it the id='3' is not unique.


>            <xs:element name="a">
>              <xs:complexType>
>                <xs:attribute name="id" use="required"/>
>              </xs:complexType>
>              <xs:unique name="uniqueId-a">
>                <xs:selector xpath="*"/>
>                <xs:field xpath="@id"/>
>              </xs:unique>
>            </xs:element>

  Well, the value of the attribute @id is unique within the scope of each element a.  What you're looking for, most likely, is to have unique values for @id of all elements a children of an Item, an similar for elements b.  In this case, the following schema should do that:

   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
              elementFormDefault="qualified">
      <xs:element name="Item">
         <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
               <xs:choice>
                  <xs:element name="a">
                     <xs:complexType>
                        <xs:attribute name="id" use="required"/>
                     </xs:complexType>
                  </xs:element>
                  <xs:element name="b">
                     <xs:complexType>
                        <xs:attribute name="id" use="required"/>
                     </xs:complexType>
                  </xs:element>
               </xs:choice>
            </xs:sequence>
         </xs:complexType>
         <xs:unique name="uniqueId-a">
            <xs:selector xpath="a"/>
            <xs:field xpath="@id"/>
         </xs:unique>
         <xs:unique name="uniqueId-b">
            <xs:selector xpath="b"/>
            <xs:field xpath="@id"/>
         </xs:unique>
      </xs:element>
   </xs:schema>

  Regards,


-- 
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/


More information about the oXygen-user mailing list