473,473 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

xsd schemas - how to...

Hi,

I have to specify that a person needs to have at least one out of three
phone elements:

<Phone type="home"/>,
<Phone type="work"/>,
<Phone type="mobile"/>,

Can anyone please suggest how to define xsd schema to enforce such a rule
(note that one type cannot be specified more than once).

Thanks in advance,

Sergey
Nov 12 '05 #1
5 1141


Sergey Poberezovskiy wrote:

I have to specify that a person needs to have at least one out of three
phone elements:

<Phone type="home"/>,
<Phone type="work"/>,
<Phone type="mobile"/>,


The schema below defines a simple type enumerating the possible phone
types and then defines other elements.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:simpleType name="phoneType">
<xs:restriction base="xs:string">
<xs:enumeration value="home" />
<xs:enumeration value="work" />
<xs:enumeration value="mobile" />
</xs:restriction>
</xs:simpleType>

<xs:element name="Persons">
<xs:complexType>
<xs:sequence>
<xs:element name="Person" type="PersonType"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Phone-Numbers">
<xs:complexType>
<xs:sequence>
<xs:element name="Phone" maxOccurs="10">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" type="phoneType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

</xs:schema>

An example document is

<?xml version="1.0" encoding="UTF-8"?>
<Persons
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2005102401Xsd.x ml">
<Person>
<Name>Prename1 Surname1</Name>
<Phone-Numbers>
<Phone type="home">12345</Phone>
</Phone-Numbers>
</Person>
<Person>
<Name>Prename2 Surname2</Name>
<Phone-Numbers>
<Phone type="work">12345</Phone>
</Phone-Numbers>
</Person>
<Person>
<Name>Prename3 Surname3</Name>
<Phone-Numbers>
<Phone type="mobile">12345</Phone>
</Phone-Numbers>
</Person>
<Person>
<Name>Prename4 Surname4</Name>
<Phone-Numbers>
</Phone-Numbers>
</Person>
</Persons>

were a validating parser will flag the Phone-Numbers element in the last
Person element as an error as it needs to have at least one Phone child
element.

I don't know how many phone numbers you want to store for one person,
the schema above allows 1 to 10 as an example.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Martin,

As in previous post I need all three phones to be different (cannot have
more than one home, for example) with at least one specified.
"Martin Honnen" wrote:


Sergey Poberezovskiy wrote:

I have to specify that a person needs to have at least one out of three
phone elements:

<Phone type="home"/>,
<Phone type="work"/>,
<Phone type="mobile"/>,


The schema below defines a simple type enumerating the possible phone
types and then defines other elements.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:simpleType name="phoneType">
<xs:restriction base="xs:string">
<xs:enumeration value="home" />
<xs:enumeration value="work" />
<xs:enumeration value="mobile" />
</xs:restriction>
</xs:simpleType>

<xs:element name="Persons">
<xs:complexType>
<xs:sequence>
<xs:element name="Person" type="PersonType"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Phone-Numbers">
<xs:complexType>
<xs:sequence>
<xs:element name="Phone" maxOccurs="10">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" type="phoneType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

</xs:schema>

An example document is

<?xml version="1.0" encoding="UTF-8"?>
<Persons
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2005102401Xsd.x ml">
<Person>
<Name>Prename1 Surname1</Name>
<Phone-Numbers>
<Phone type="home">12345</Phone>
</Phone-Numbers>
</Person>
<Person>
<Name>Prename2 Surname2</Name>
<Phone-Numbers>
<Phone type="work">12345</Phone>
</Phone-Numbers>
</Person>
<Person>
<Name>Prename3 Surname3</Name>
<Phone-Numbers>
<Phone type="mobile">12345</Phone>
</Phone-Numbers>
</Person>
<Person>
<Name>Prename4 Surname4</Name>
<Phone-Numbers>
</Phone-Numbers>
</Person>
</Persons>

were a validating parser will flag the Phone-Numbers element in the last
Person element as an error as it needs to have at least one Phone child
element.

I don't know how many phone numbers you want to store for one person,
the schema above allows 1 to 10 as an example.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #3
You need to use uniqueness constraint for that. Below is an example:

<xs:schema targetNamespace="http://myNS"
elementFormDefault="qualified"
xmlns="http://myNS"
xmlns:p="http://myNS"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="phoneType">
<xs:restriction base="xs:string">
<xs:enumeration value="home" />
<xs:enumeration value="work" />
<xs:enumeration value="mobile" />
</xs:restriction>
</xs:simpleType>

<xs:complexType name="phone">
<xs:attribute name="type" type="phoneType" use="required"/>
</xs:complexType>

<xs:element name="phoneNumbers">
<xs:complexType>
<xs:sequence>
<xs:element name="Phone" type="phone" minOccurs="1" maxOccurs="3"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="phoneTypes">
<xs:selector xpath="p:Phone"/>
<xs:field xpath="@type"/>
</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
"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.com> wrote
in message news:5B**********************************@microsof t.com...
Hi,

I have to specify that a person needs to have at least one out of three
phone elements:

<Phone type="home"/>,
<Phone type="work"/>,
<Phone type="mobile"/>,

Can anyone please suggest how to define xsd schema to enforce such a rule
(note that one type cannot be specified more than once).

Thanks in advance,

Sergey

Nov 12 '05 #4
You can make use of xs:unique constraint here. Make the Person your context
node, and put the unique constraint on the @type attribute so that no values
are repeated under <Person>

"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.com> wrote
in message news:B0**********************************@microsof t.com...
Martin,

As in previous post I need all three phones to be different (cannot have
more than one home, for example) with at least one specified.
"Martin Honnen" wrote:


Sergey Poberezovskiy wrote:

I have to specify that a person needs to have at least one out of three phone elements:

<Phone type="home"/>,
<Phone type="work"/>,
<Phone type="mobile"/>,


The schema below defines a simple type enumerating the possible phone
types and then defines other elements.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:simpleType name="phoneType">
<xs:restriction base="xs:string">
<xs:enumeration value="home" />
<xs:enumeration value="work" />
<xs:enumeration value="mobile" />
</xs:restriction>
</xs:simpleType>

<xs:element name="Persons">
<xs:complexType>
<xs:sequence>
<xs:element name="Person" type="PersonType"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Phone-Numbers">
<xs:complexType>
<xs:sequence>
<xs:element name="Phone" maxOccurs="10">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" type="phoneType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

</xs:schema>

An example document is

<?xml version="1.0" encoding="UTF-8"?>
<Persons
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2005102401Xsd.x ml">
<Person>
<Name>Prename1 Surname1</Name>
<Phone-Numbers>
<Phone type="home">12345</Phone>
</Phone-Numbers>
</Person>
<Person>
<Name>Prename2 Surname2</Name>
<Phone-Numbers>
<Phone type="work">12345</Phone>
</Phone-Numbers>
</Person>
<Person>
<Name>Prename3 Surname3</Name>
<Phone-Numbers>
<Phone type="mobile">12345</Phone>
</Phone-Numbers>
</Person>
<Person>
<Name>Prename4 Surname4</Name>
<Phone-Numbers>
</Phone-Numbers>
</Person>
</Persons>

were a validating parser will flag the Phone-Numbers element in the last
Person element as an error as it needs to have at least one Phone child
element.

I don't know how many phone numbers you want to store for one person,
the schema above allows 1 to 10 as an example.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #5
Stan and Zafar,

Thanks a lot for your response.

If you could please answer the question from the previous post - that would
be terrific:

I have to specify that at least one out of two elements must exist (both is
fine), but no more than one of each - silimar to the example in this code. I
have come up with a schema that seems to enforce these rules - the only
problem is that I cannot validate against it in .Net - when both elements are
specified - the validation fails. Please see my previous post for more info.
"Stan Kitsis [MSFT]" wrote:
You need to use uniqueness constraint for that. Below is an example:

<xs:schema targetNamespace="http://myNS"
elementFormDefault="qualified"
xmlns="http://myNS"
xmlns:p="http://myNS"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="phoneType">
<xs:restriction base="xs:string">
<xs:enumeration value="home" />
<xs:enumeration value="work" />
<xs:enumeration value="mobile" />
</xs:restriction>
</xs:simpleType>

<xs:complexType name="phone">
<xs:attribute name="type" type="phoneType" use="required"/>
</xs:complexType>

<xs:element name="phoneNumbers">
<xs:complexType>
<xs:sequence>
<xs:element name="Phone" type="phone" minOccurs="1" maxOccurs="3"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="phoneTypes">
<xs:selector xpath="p:Phone"/>
<xs:field xpath="@type"/>
</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
"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.com> wrote
in message news:5B**********************************@microsof t.com...
Hi,

I have to specify that a person needs to have at least one out of three
phone elements:

<Phone type="home"/>,
<Phone type="work"/>,
<Phone type="mobile"/>,

Can anyone please suggest how to define xsd schema to enforce such a rule
(note that one type cannot be specified more than once).

Thanks in advance,

Sergey


Nov 12 '05 #6

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

Similar topics

0
by: kyancy | last post by:
Hello All. We have several XML schemas to describe common component document parts. We then create new XML schemas as necessary that use "xsd:import schemaLocation=whateverLocation.." to include...
30
by: btober | last post by:
Whenever I create a temporary table, with something like CREATE TEMPORARY TABLE temptable1 AS SELECT * FROM paid.ad_hoc_query; New schemas appear, with names like "pg_temp_1". I guess the...
7
by: Roderick A. Anderson | last post by:
I'm looking for some input on a configuration I'm implementing. The long term goal is to providing hosting for companies and organizations with a basic/generic set of applications that use...
4
by: anonymous | last post by:
When I use the schema collection to apply many schemas to one XML instance document, I get an error if I do not qualify every element with the appropriate namespace. Both the W3C site and this...
2
by: John Jenkins | last post by:
Hi, I have a lot of schemas to load into a schema collection. I load them in by reading each one into a XMLTextReader from disk and add them into a schema collection. I have a couple of issues to...
1
by: CSN | last post by:
I have two machines between which I exchange dumps a lot. On the first (Windows/cygwin), pgsql was set up with "Administrator" as the main superuser - who owns all schemas in template0 and...
6
by: Dennis Gearon | last post by:
This post is as much about getting some questions answered as leaving the following definitions in the archives for the next person. After a quick perview of the web, I came up with the...
0
by: Net Virtual Mailing Lists | last post by:
I've been spending the last few days converting many databases into a single schema and have completed the process, but now I'm at somewhat of an impasse as to the best way to proceed forward.... ...
3
by: Sami Marzouki | last post by:
Hi, What I'm trying to do is: - To write a Web.config with custom sections. (Done) - To write a xsd schema for this custom sections.(Done) - Tell the Web.config to take the two schemas. When...
0
by: vihrao | last post by:
I am designing wsdl that uses multiple schemas. I can do this in two ways: 1) use multiple schema imports in one wsdl or 2) use multiple schema imports in to one common schema and then import a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
1
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.