473,671 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>Movie s</Interest>
<Interest>Compu ters</Interest>
</Person>
<Person>
<Interest>Movie s</Interest>
</Person>

Non-valid:
<Person>
<Interest>Movie s</Interest>
<Interest>Movie s</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 1597
I'm not sure. But I know there is a "unique" keyword in schema for this
purpose.
bo********@hotm ail.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>Movie s</Interest>
<Interest>Compu ters</Interest>
</Person>
<Person>
<Interest>Movie s</Interest>
</Person>

Non-valid:
<Person>
<Interest>Movie s</Interest>
<Interest>Movie s</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"

elementFormDefa ult="qualified"

xmlns="foo"

xmlns:foo="foo"

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

<xs:simpleTyp e name="interestT ype">

<xs:restricti on base="xs:string ">

<xs:enumerati on value="Movies"/>

<xs:enumerati on value="Computer s"/>

</xs:restriction>

</xs:simpleType>

<xs:complexTy pe name="personTyp e">

<xs:sequence>

<xs:element name="Interest" type="interestT ype"
maxOccurs="unbo unded"/>

</xs:sequence>

</xs:complexType>

<xs:element name="Person" type="personTyp e">

<xs:unique name="uniqueInt erests">

<xs:selector xpath="foo:Inte rest"/>

<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********@hot mail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I've tried several ways to achieve a xsd schema for the following xml
example, but failed to do so.

Valid:
<Person>
<Interest>Movie s</Interest>
<Interest>Compu ters</Interest>
</Person>
<Person>
<Interest>Movie s</Interest>
</Person>

Non-valid:
<Person>
<Interest>Movie s</Interest>
<Interest>Movie s</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="unbo unded">
<xs:complexType >
<xs:sequence>
<xs:element ref="Interest"
type="InterestE numType"
minOccurs="0" maxOccurs="unbo unded"/>
</xs:sequence>
</xs:complexType>

<xs:unique name="UniqueInt erestPerPersonK ey">
<xs:select xpath="."/>
<xs:field xpath="Interest "/>
</xs:unique>

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

<xs:simpleTyp e>
<xs:restricti on base="xs:string ">
<xs:enumerati on value="Computer s"/>
<xs:enumerati on value="Movies"/>
</xs:restriction>
</xs:simpleType>
On 28 Jul 2005 15:09:41 -0700, bo********@hotm ail.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>Movie s</Interest>
<Interest>Compu ters</Interest>
</Person>
<Person>
<Interest>Movie s</Interest>
</Person>

Non-valid:
<Person>
<Interest>Movie s</Interest>
<Interest>Movie s</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="unbo unded">
<xs:complexType >
<xs:sequence>
<xs:element ref="Interest"
type="InterestE numType"
minOccurs="0" maxOccurs="unbo unded"/>
</xs:sequence>
</xs:complexType>

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

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

<xs:simpleType >
<xs:restricti on base="xs:string ">
<xs:enumerati on value="Computer s"/>
<xs:enumerati on value="Movies"/>
</xs:restriction>
</xs:simpleType>
On 28 Jul 2005 15:09:41 -0700, bo********@hotm ail.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>Movie s</Interest>
<Interest>Compu ters</Interest>
</Person>
<Person>
<Interest>Movie s</Interest>
</Person>

Non-valid:
<Person>
<Interest>Movie s</Interest>
<Interest>Movie s</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********@hotm ail.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>Movie s</Interest>
<Interest>Compu ters</Interest>
</Person>
<Person>
<Interest>Movie s</Interest>
</Person>

Non-valid:
<Person>
<Interest>Movie s</Interest>
<Interest>Movie s</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"
elementFormDefa ult="qualified"
attributeFormDe fault="unqualif ied"
xmlns="uri:fo2"
xmlns:fo2="uri: fo2">

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

<xs:element name="Person">
<xs:complexType >
<xs:sequence>
<xs:element name="Interest"
type="InterestE numType"
minOccurs="0" maxOccurs="unbo unded"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="PersonDis tinctInterests" >
<xs:selector xpath="fo2:Inte rest"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>

<xs:simpleTyp e name="InterestE numType">
<xs:restricti on base="xs:string ">
<xs:enumerati on value="Computer s"/>
<xs:enumerati on 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:schemaLocat ion="uri:fo2 fo2.xsd"
xmlns:fo2="uri: fo2"
xmlns="uri:fo2" >

<Person>
<Interest>Movie s</Interest>
<Interest>Compu ters</Interest>
</Person>

<Person>
<Interest>Movie s</Interest>
<Interest>Compu ters</Interest>
</Person>

<Person>
<Interest>Movie s</Interest>
<Interest>Movie s</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
3320
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 schema instance.
0
4223
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> > <password>brown</password> > </userlogin> > Above is my schema instance.
1
2711
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 is a subset of the other. I will describe one part of the schema to give you an idea of what my problem is. The "big" schema calls for 0 or more selector elements, each containing a (required) name attribute and simple character data, as...
1
4617
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 the same
1
1480
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> </TUPLE>
5
2237
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 possible? Note that both the namespaces should be in the same schema and same xsd file. Could somebody provide a small snippet on how to do this? Thanks for your time.
1
1237
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 the new schema files and this works fine. However, I was wondering if it is recommended for me to declare elements in this 'common' schema as some of the elements are common also? I ask this because I have read through some newgroups and they
9
3008
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> <binaryobject3></binaryobject3>
0
1320
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 name="B" type="string" maxOccurs="10"/> <element name="C" type="string"/> </sequence> </complexType>
0
8926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8824
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7444
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6236
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.