473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XML Schema Repeated Elements

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 '05 #1
6 1578
I'm not sure. But I know there is a "unique" keyword in schema for this
purpose.
bo********@hotmail.com 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

Jul 29 '05 #2
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

<bo********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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

Jul 29 '05 #3
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, bo********@hotmail.com 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


Jul 29 '05 #4
On Thu, 28 Jul 2005 19:17:05 -0700, Steve Jorgensen <no****@nospam.nospam>
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, bo********@hotmail.com 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


Jul 29 '05 #5
On 28 Jul 2005 15:09:41 -0700, bo********@hotmail.com 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>

Jul 29 '05 #6
Thank you all,

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

With regards,

Hans Bos

Jul 29 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: wooks | last post by:
<?xml version='1.0'?> <userlogin xmlns="urn:faster:userlogin" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <login>mick</login> <password>brown</password> </userlogin> Above is my...
0
by: C. M. Sperberg-McQueen | last post by:
wooks (wookiz@hotmail.com) wrote: > <?xml version='1.0'?> > <userlogin xmlns="urn:faster:userlogin" > xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> > <login>mick</login> > ...
1
by: Gregg Williams | last post by:
Hi--I am having a problem designing a schema to fit my XML data, and I'm hoping that someone can help. Essentially, I have a schema in mind and two target vocabularies for it, where one vocabulary...
1
by: ruthless | last post by:
hello. i've got a question can i in XML Schema define tag that works as lists knows from e.g. C,C++ - recurrent tags? i'm talking about e.g. genealogical tree every level of this tree is...
1
by: Patrícia Martins | last post by:
Hi, I would like to know if it's possible to validate attributes in a situation like this: <TUPLE> <LIST type="type1">Bla</LIST> <LIST type="type2">Ble</LIST> <ATOM type="type3">Bli</ATOM>...
5
by: Zombie | last post by:
Hi, Can I have 2 namespaces in the same XML schema? In the schema, I wish to declare elements such that some of them belong to one namespace and others belong to a second namespace. Is this...
1
by: j.tremlett | last post by:
Hi, I have now drafted a few small-scale schemas and have various types being repeated. I have read that using the 'include' element will allow me to write these once and then call them from...
9
by: mstilli | last post by:
Hi, I am trying to use schema for server side validation using xerces to catch the validation errors. validating this XML: <Content4> <textarea13></textarea13>...
0
by: ChipR | last post by:
Is there any way to specify a sequence with one or more elements that can be repeated like <complexType name="element_type"> <sequence> <element name="A" type="string"/> <element...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.