xsd: unique name element

Sebastian Kerekes

Usenet Archive
Joined
Sep 1, 2004
Messages
16
Reaction score
0
Greetings,

I'm new to XSD and I'm trying to create a schema for the following: The document 'persons' should contain 0+ elements of type 'person', which has a subelement called 'name' which is a complex type consisting of the elements 'firstname' and 'lastname'. 'name' should be unique, no combination of firstname and lastname should appear twice.

And that's where I fail, at least XMLSpy tells my, that my code is not correct. I had a look at some sources in the internet, to me the code seems correct ..

Could someone have a look at the following code and tell my what my fault is?

Thanks for you answers in advance,

Sebastian

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="persons"> <xs:complexType> <xs:all> <xs:element name="person"> <xs:unique> <xs:selector xpath="."/> <xs:field xpath="./name/firstname"/> </xs:unique> <xs:complexType> <xs:all> <xs:element name="name" type="name"/> </xs:all> </xs:complexType> </xs:element> </xs:all> </xs:complexType> </xs:element> <xs:complexType name="name"> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType> </xs:schema>
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Sebastian said:
I'm new to XSD and I'm trying to create a schema for the following: The document 'persons' should contain 0+ elements of type 'person', which has a subelement called 'name' which is a complex type consisting of the elements 'firstname' and 'lastname'. 'name' should be unique, no combination of firstname and lastname should appear twice.

Here is a working schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" type="personType" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> <xs:unique name="uniquePersonsByName"> <xs:selector xpath="person" /> <xs:field xpath="name/firstname" /> <xs:field xpath="name/lastname" /> </xs:unique> </xs:element>

<xs:complexType name="personType">
<xs:sequence>
<xs:element name="name" type="nameType" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="nameType">
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
</xs:sequence>
</xs:complexType>

</xs:schema>

And here an instance that causes a validation error:

<?xml version="1.0" encoding="UTF-8"?>
<persons
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test2004110201Xsd.xml"> <person> <name> <firstname>Martin</firstname> <lastname>Honnen</lastname> </name> </person> <person> <name> <firstname>Michael</firstname> <lastname>Honnen</lastname> </name> </person> <person> <name> <firstname>Martin</firstname> <lastname>Honnen</lastname> </name> </person> </persons>
 

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

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top