NEWBIE: Enforcing uniqueness in XML schema

V

Victor

Hi, I am pulling my hair out on this one.
I have written an XML schema to ensure that a person name occurs only
once (ie it is unique). The XML has the name Christophe once, and
Victor twice, but it appears valid allowing Victor twice. How come, I
cannot see my bug? I know the schema is validating the XML as if I
enter 4 names (and not 3 or less), it is showing up invalid.
Thank you for thinking on this.
Victor



<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="addressBook">
<xs:complexType>
<xs:sequence>
<xs:element name="personName" maxOccurs="3">
<xs:unique name="dummy">
<xs:selector xpath="addressBook/personName"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>




<?xml version="1.0" encoding="UTF-8"?>
<addressBook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\Documents and
Settings\VCuklin\Desktop\XMLSPYTest\test.xsd">
<personName>Victor</personName>
<personName>Christophe</personName>
<personName>Victor</personName>
</addressBook>
 
M

Martin Honnen

Victor said:
I have written an XML schema to ensure that a person name occurs only
once (ie it is unique). The XML has the name Christophe once, and
Victor twice, but it appears valid allowing Victor twice. How come, I
cannot see my bug? I know the schema is validating the XML as if I
enter 4 names (and not 3 or less), it is showing up invalid.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="addressBook">
<xs:complexType>
<xs:sequence>
<xs:element name="personName" maxOccurs="3">
<xs:unique name="dummy">
<xs:selector xpath="addressBook/personName"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

The placement is wrong (and you need to type the personName element, use

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="addressBook">
<xs:complexType>
<xs:sequence>
<xs:element name="personName" maxOccurs="3" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:unique name="dummy">
<xs:selector xpath="personName"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
</xs:schema>

that should do what you need.
 
V

Victor

Martin thank you for your help yesterday. It works. My first example
was simple and I just wanted the syntax to enforce uniqueness.
Following your example I can't get what I really want to work work and
should have posted this example instead (XML in Nutshell is vague with
no examples).

Here is the second lot of XML I am trying to validate, All I want is
the person to have many children but they must have unique names, it
this is invalid because Niklas appears twice. The FamilyDetail will be
referenced from more than one place in my large schema.

Thank you again.
Victor


<?xml version="1.0" encoding="UTF-8"?>
<addressBook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\Documents and
Settings\VCuklin\Desktop\XMLSPYTest\test.xsd">
<personName>Victor</personName>
<FamilyDetail>
<wifesName>Hanna</wifesName>
<childrenName>Christoph</childrenName>
<childrenName>Niklas</childrenName>
<childrenName>Niklas</childrenName>
</FamilyDetail>
</addressBook>


And here is my schema that doesn't enforce unique children names which
is what I want.



<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="addressBook">

<xs:complexType>
<xs:sequence>
<xs:element name="personName" type="xs:string"/>
<xs:element ref="FamilyDetail"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="FamilyDetail">
<xs:complexType>
<xs:sequence>
<xs:element name="wifesName" type="xs:string"/>
<xs:element name="childrenName" type="xs:string"
maxOccurs="unbounded">
<xs:unique name="dummy">
<xs:selector xpath="childrenName"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>
 
M

Martin Honnen

Victor said:
Martin thank you for your help yesterday. It works. My first example
was simple and I just wanted the syntax to enforce uniqueness.
Following your example I can't get what I really want to work work and
should have posted this example instead (XML in Nutshell is vague with
no examples).

Here is the second lot of XML I am trying to validate, All I want is
the person to have many children but they must have unique names, it
this is invalid because Niklas appears twice. The FamilyDetail will be
referenced from more than one place in my large schema.

Well, you should have learned from my first correction that the
placement of the xs:unique element matters, it belongs into the element
definition for which you want to ensure uniqueness of the contained element:
http://www.w3.org/TR/xmlschema-0/#specifyingUniqueness
<?xml version="1.0" encoding="UTF-8"?>
<addressBook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\Documents and
Settings\VCuklin\Desktop\XMLSPYTest\test.xsd">
<personName>Victor</personName>
<FamilyDetail>
<wifesName>Hanna</wifesName>
<childrenName>Christoph</childrenName>
<childrenName>Niklas</childrenName>
<childrenName>Niklas</childrenName>
</FamilyDetail>
</addressBook>

Thus a corrected schema is

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="addressBook">

<xs:complexType>
<xs:sequence>
<xs:element name="personName" type="xs:string"/>
<xs:element ref="FamilyDetail" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="FamilyDetail">
<xs:complexType>
<xs:sequence>
<xs:element name="wifesName" type="xs:string"/>
<xs:element name="childrenName" type="xs:string"
maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="dummy">
<xs:selector xpath="childrenName"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>

</xs:schema>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top