XML Schema Repeated Elements

B

bosgoverde

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
 
S

Stan Kitsis [MSFT]

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
 
S

Steve Jorgensen

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

Steve Jorgensen

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

Steve Jorgensen

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

bosgoverde

Thank you all,

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

With regards,

Hans Bos
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top