
I'd appreciate help figuring out why the schema below validates the following xml file even though it the id='3' is not unique. Regards, - Olumide --------------------------------------------------------- <?xml version="1.0" encoding="UTF-8"?> <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:unique name="uniqueId-a"> <xs:selector xpath="*"/> <xs:field xpath="@id"/> </xs:unique> </xs:element> <xs:element name="b"> <xs:complexType> <xs:attribute name="id" use="required"/> </xs:complexType> <xs:unique name="uniqueId-b"> <xs:selector xpath="*"/> <xs:field xpath="@id"/> </xs:unique> </xs:element> </xs:choice> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> ---------------------------------------------------- <?xml version="1.0" encoding="UTF-8"?> <Item> <a id="3"/> <!-- shared id --> <a id="2"/> <b id="3"/> <!-- shared id --> <a id="4"/> <b id="0"/> <b id="3"/> <!-- shared id --> </Item>

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/

On 29/11/2012 18:00, Florent Georges wrote:
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:
Thanks. I've changed the <xs:unique>'s as follows because they can also be nested in <Item>. Is this correct? <xs:unique name="uniqueId-a"> <xs:selector xpath="*"/> <xs:field xpath="@id"/> </xs:unique> <xs:unique name="uniqueId-b"> <xs:selector xpath="*"/> <xs:field xpath="@id"/> </xs:unique> - Olumide

On 29/11/2012 18:09, Olumide wrote:
Thanks. I've changed the <xs:unique>'s as follows because they can also be nested in <Item>. Is this correct?
Scratch that. Here's my latest attempt <xs:unique name="UniqueIdConstraint"> <xs:selector xpath=".//*"/> <xs:field xpath="@id"/> </xs:unique> - Olumide

Olumide wrote:
<xs:unique name="UniqueIdConstraint"> <xs:selector xpath=".//*"/> <xs:field xpath="@id"/> </xs:unique>
If @id must be unique across all descendent elements, yes. If the IDs must be unique across all descendent a elements on the one hand, and unique across all b elements on the other hand, then you need two xs:unique, and change .//* to resp. .//a and .//b. Regards, -- Florent Georges http://fgeorges.org/ http://h2oconsulting.be/
participants (2)
-
Florent Georges
-
Olumide