Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML Schema Repeated Elements

Reply
Thread Tools

XML Schema Repeated Elements

 
 
bosgoverde@hotmail.com
Guest
Posts: n/a
 
      07-28-2005
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

 
Reply With Quote
 
 
 
 
James Gan
Guest
Posts: n/a
 
      07-29-2005
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
>

 
Reply With Quote
 
 
 
 
Stan Kitsis [MSFT]
Guest
Posts: n/a
 
      07-29-2005
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"

xmlnss="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: oups.com...
> 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
>



 
Reply With Quote
 
Steve Jorgensen
Guest
Posts: n/a
 
      07-29-2005
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


 
Reply With Quote
 
Steve Jorgensen
Guest
Posts: n/a
 
      07-29-2005
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


 
Reply With Quote
 
Steve Jorgensen
Guest
Posts: n/a
 
      07-29-2005
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
xmlnss="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
xmlnssi="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>

 
Reply With Quote
 
bosgoverde@hotmail.com
Guest
Posts: n/a
 
      07-29-2005
Thank you all,

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

With regards,

Hans Bos

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
web.xml / XML schema issue, why do some XML schema attributes disappear asciz@starmail.com Java 3 02-20-2007 09:56 AM
Validation with XSD using XML::LibXML::Schema, and XML::Validator::Schema huntingseasonson@gmail.com Perl Misc 5 11-29-2006 12:37 PM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
XML Schema to XML Schema Conversion Hari Om XML 1 03-02-2004 09:04 PM
XML schema regular expressions question and recommended XML Schema book Fred Smith XML 1 02-05-2004 11:12 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57