xsd: unique name element

Discussion in 'XML' started by Sebastian Kerekes, Nov 2, 2004.

  1. 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>
    Sebastian Kerekes, Nov 2, 2004
    #1
    1. Advertising

  2. Sebastian Kerekes wrote:

    > 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>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Nov 2, 2004
    #2
    1. Advertising

  3. Martin Honnen wrote:

    > Here is a working schema:
    > [..]
    > And here an instance that causes a validation error:
    > [..]


    Thank you very much! :)

    Sebastian
    Sebastian Kerekes, Nov 2, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthet
    Replies:
    2
    Views:
    662
    Matthet
    Sep 24, 2003
  2. Mike
    Replies:
    1
    Views:
    433
  3. ToshiBoy
    Replies:
    6
    Views:
    832
    ToshiBoy
    Aug 12, 2008
  4. PeteOlcott
    Replies:
    1
    Views:
    934
    Mukul Gandhi
    Feb 5, 2009
  5. Replies:
    6
    Views:
    155
Loading...

Share This Page