XML Schema Repeated Elements

Discussion in 'XML' started by bosgoverde@hotmail.com, Jul 28, 2005.

  1. Guest

    I've tried several ways to achieve a xsd schema for the following xml
    example, but failed to do so.

    Valid:
    <Person>
    <Interest>Movies</Interest>
    <Interest>Computers</Interest>
    </Person>
    <Person>
    <Interest>Movies</Interest>
    </Person>

    Non-valid:
    <Person>
    <Interest>Movies</Interest>
    <Interest>Movies</Interest>
    </Person>

    In this example the Interests are an enumeration.
    Is it possible to make a restriction for the repeated elements so that
    all elements have a different value?

    All help is appreciated,

    Hans
     
    , Jul 28, 2005
    #1
    1. Advertising

  2. James Gan Guest

    I'm not sure. But I know there is a "unique" keyword in schema for this
    purpose.


    wrote:
    > I've tried several ways to achieve a xsd schema for the following xml
    > example, but failed to do so.
    >
    > Valid:
    > <Person>
    > <Interest>Movies</Interest>
    > <Interest>Computers</Interest>
    > </Person>
    > <Person>
    > <Interest>Movies</Interest>
    > </Person>
    >
    > Non-valid:
    > <Person>
    > <Interest>Movies</Interest>
    > <Interest>Movies</Interest>
    > </Person>
    >
    > In this example the Interests are an enumeration.
    > Is it possible to make a restriction for the repeated elements so that
    > all elements have a different value?
    >
    > All help is appreciated,
    >
    > Hans
    >
     
    James Gan, Jul 29, 2005
    #2
    1. Advertising

  3. Hi Hans,

    The following schema does what you want.

    <?xml version="1.0" encoding="utf-8" ?>

    <xs:schema targetNamespace="foo"

    elementFormDefault="qualified"

    xmlns="foo"

    xmlns:foo="foo"

    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:simpleType name="interestType">

    <xs:restriction base="xs:string">

    <xs:enumeration value="Movies"/>

    <xs:enumeration value="Computers"/>

    </xs:restriction>

    </xs:simpleType>



    <xs:complexType name="personType">

    <xs:sequence>

    <xs:element name="Interest" type="interestType"
    maxOccurs="unbounded"/>

    </xs:sequence>

    </xs:complexType>

    <xs:element name="Person" type="personType">

    <xs:unique name="uniqueInterests">

    <xs:selector xpath="foo:Interest"/>

    <xs:field xpath="."/>

    </xs:unique>

    </xs:element>

    </xs:schema>


    --
    Stan Kitsis
    Program Manager, XML Technologies
    Microsoft Corporation

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm

    <> wrote in message
    news:...
    > I've tried several ways to achieve a xsd schema for the following xml
    > example, but failed to do so.
    >
    > Valid:
    > <Person>
    > <Interest>Movies</Interest>
    > <Interest>Computers</Interest>
    > </Person>
    > <Person>
    > <Interest>Movies</Interest>
    > </Person>
    >
    > Non-valid:
    > <Person>
    > <Interest>Movies</Interest>
    > <Interest>Movies</Interest>
    > </Person>
    >
    > In this example the Interests are an enumeration.
    > Is it possible to make a restriction for the repeated elements so that
    > all elements have a different value?
    >
    > All help is appreciated,
    >
    > Hans
    >
     
    Stan Kitsis [MSFT], Jul 29, 2005
    #3
  4. Off the top of my head (and my syntax could be slightly off), ...

    ....
    <xs:sequence>
    <xs:element name="Person" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="Interest"
    type="InterestEnumType"
    minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>

    <xs:unique name="UniqueInterestPerPersonKey">
    <xs:select xpath="."/>
    <xs:field xpath="Interest"/>
    </xs:unique>

    </xs:element>
    </xs:sequence>
    ....

    <xs:simpleType>
    <xs:restriction base="xs:string">
    <xs:enumeration value="Computers"/>
    <xs:enumeration value="Movies"/>
    </xs:restriction>
    </xs:simpleType>


    On 28 Jul 2005 15:09:41 -0700, wrote:

    >I've tried several ways to achieve a xsd schema for the following xml
    >example, but failed to do so.
    >
    >Valid:
    ><Person>
    > <Interest>Movies</Interest>
    > <Interest>Computers</Interest>
    ></Person>
    ><Person>
    > <Interest>Movies</Interest>
    ></Person>
    >
    >Non-valid:
    ><Person>
    > <Interest>Movies</Interest>
    > <Interest>Movies</Interest>
    ></Person>
    >
    >In this example the Interests are an enumeration.
    >Is it possible to make a restriction for the repeated elements so that
    >all elements have a different value?
    >
    >All help is appreciated,
    >
    >Hans
     
    Steve Jorgensen, Jul 29, 2005
    #4
  5. On Thu, 28 Jul 2005 19:17:05 -0700, Steve Jorgensen <>
    wrote:

    >Off the top of my head (and my syntax could be slightly off), ...
    >
    >...


    ....

    Oops - I just realized why that unique constraint is wrong - and why this case
    is confusing. This one might work. I'll test it out later and post back.

    <xs:sequence>
    <xs:element name="Person" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="Interest"
    type="InterestEnumType"
    minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>

    <xs:unique name="UniqueInterestsPerPerson">
    <xs:select xpath="Interest"/>
    <xs:field xpath="."/>
    </xs:unique>

    </xs:element>
    </xs:sequence>
    >...
    >
    ><xs:simpleType>
    > <xs:restriction base="xs:string">
    > <xs:enumeration value="Computers"/>
    > <xs:enumeration value="Movies"/>
    > </xs:restriction>
    ></xs:simpleType>
    >
    >
    >On 28 Jul 2005 15:09:41 -0700, wrote:
    >
    >>I've tried several ways to achieve a xsd schema for the following xml
    >>example, but failed to do so.
    >>
    >>Valid:
    >><Person>
    >> <Interest>Movies</Interest>
    >> <Interest>Computers</Interest>
    >></Person>
    >><Person>
    >> <Interest>Movies</Interest>
    >></Person>
    >>
    >>Non-valid:
    >><Person>
    >> <Interest>Movies</Interest>
    >> <Interest>Movies</Interest>
    >></Person>
    >>
    >>In this example the Interests are an enumeration.
    >>Is it possible to make a restriction for the repeated elements so that
    >>all elements have a different value?
    >>
    >>All help is appreciated,
    >>
    >>Hans
     
    Steve Jorgensen, Jul 29, 2005
    #5
  6. On 28 Jul 2005 15:09:41 -0700, wrote:

    >I've tried several ways to achieve a xsd schema for the following xml
    >example, but failed to do so.
    >
    >Valid:
    ><Person>
    > <Interest>Movies</Interest>
    > <Interest>Computers</Interest>
    ></Person>
    ><Person>
    > <Interest>Movies</Interest>
    ></Person>
    >
    >Non-valid:
    ><Person>
    > <Interest>Movies</Interest>
    > <Interest>Movies</Interest>
    ></Person>
    >
    >In this example the Interests are an enumeration.
    >Is it possible to make a restriction for the repeated elements so that
    >all elements have a different value?
    >
    >All help is appreciated,
    >
    >Hans


    I have now tested the following schema, and it works...

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

    <xs:element name="Root">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="Person"
    minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    <xs:element name="Person">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Interest"
    type="InterestEnumType"
    minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    <xs:unique name="PersonDistinctInterests">
    <xs:selector xpath="fo2:Interest"/>
    <xs:field xpath="."/>
    </xs:unique>
    </xs:element>

    <xs:simpleType name="InterestEnumType">
    <xs:restriction base="xs:string">
    <xs:enumeration value="Computers"/>
    <xs:enumeration value="Movies"/>
    </xs:restriction>
    </xs:simpleType>

    </xs:schema>

    In the example below, an error occurs on the second <Interest> having a value
    of "Movies" in the final <Person> as a result of the xs:unique constraint.

    Here's what's happening:

    Because the unique constraint is defined within the Person element, the scope
    of the constraint is the content of an individual Person element (descendants
    of different <Person>s will not be compared to each other). <selection>
    supplies an xpath expression of what self-or-descendant elements of the
    <Person> need to be checked against each other for uniqueness, and the
    combination of <field> items (in this case, 1 of them) indicate the
    combination of self-or-descendant nodes of each selection containing the
    combination of values used to determine uniqueness by. In this case, we have
    one field (".") which is the element itself.

    <?xml version="1.0" encoding="UTF-8"?>
    <fo2:Root
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="uri:fo2 fo2.xsd"
    xmlns:fo2="uri:fo2"
    xmlns="uri:fo2">

    <Person>
    <Interest>Movies</Interest>
    <Interest>Computers</Interest>
    </Person>

    <Person>
    <Interest>Movies</Interest>
    <Interest>Computers</Interest>
    </Person>

    <Person>
    <Interest>Movies</Interest>
    <Interest>Movies</Interest>
    </Person>

    </fo2:Root>
     
    Steve Jorgensen, Jul 29, 2005
    #6
  7. Guest

    Thank you all,

    It works like a charm! I wasn't aware of the unique keyword.

    With regards,

    Hans Bos
     
    , Jul 29, 2005
    #7
    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. Markus
    Replies:
    1
    Views:
    1,541
    Markus
    Nov 23, 2005
  2. Fred Smith
    Replies:
    1
    Views:
    949
    Martin Honnen
    Feb 5, 2004
  3. Hari Om
    Replies:
    1
    Views:
    463
    Martin SChukrazy
    Mar 2, 2004
  4. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,265
    Stanimir Stamenkov
    Apr 25, 2005
  5. Replies:
    3
    Views:
    2,870
Loading...

Share This Page