473,385 Members | 1,620 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.

XSD / XSCHEMA Problem

I'm new to writing scheme.

And I'm breaking my teeth on this obviously easy one.
I would like to write a scheme which validates the following

<projects>
<project>
<id>text</id>
<name>text</name>
</project>
<project>
<name>text</name>
</project>
<project>
<id>text</id>
</project>
</projects>

But does not validate the following

<projects>
<project>
</project>
</projects>

Thus either the tag <id> or the tag <name> or both should be present.
The absence of both should result in non validation.

Thanx for the help,
I have already browsed lots of postings but couldn't find one which
could help me
Niko Stolwijk
Mar 13 '06 #1
6 1319
Your example generates this schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="projects">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="project"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="project">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="id"/>
<xs:element minOccurs="0" ref="name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="id" type="xs:NCName"/>
<xs:element name="name" type="xs:NCName"/>
</xs:schema>

The minOccurs restriction applies to individual elements, you cannot
make cross-element validations (in your case a logical OR). I'm afraid
XSD is not still powerful enough to do things like that.

Cheers
Lluis

Mar 13 '06 #2
What you want is
(id, name) | id | name
As this is ambiguous content (when the parser encounters id it cannot
decide if it should follow the (id, name) or the id model) you need to
write it in a non ambiguous way (XML Schema does not accept ambiguos
content) and that can be done as follows:

(id, name?) | name

That means either id optionally followed by name or name.

In XML Schema notation that will be:
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element ref="id"/>
<xs:element minOccurs="0" ref="name"/>
<xs:sequence>
<xs:element ref="name"/>
</xs:choice>
</xs:complexType>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Mar 14 '06 #3
Thanks for the schema.

I'm sad it's not possible to write it the way I would like it. So I have
decided to make an annotation in the schema. :-(

I have found simmilar solutions like yours, but they all allow the
absence of both tags, which I wanted to prevent.
Niko Stolwijk
Your example generates this schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="projects">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="project"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="project">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="id"/>
<xs:element minOccurs="0" ref="name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="id" type="xs:NCName"/>
<xs:element name="name" type="xs:NCName"/>
</xs:schema>

The minOccurs restriction applies to individual elements, you cannot
make cross-element validations (in your case a logical OR). I'm afraid
XSD is not still powerful enough to do things like that.

Cheers
Lluis

Mar 14 '06 #4
Thanks for the schema.

I'm sad it's not possible to write it the way I would like it. So I have
decided to make an annotation in the schema. :-(

I have found simmilar solutions like yours, but they all allow the
absence of both tags, which I wanted to prevent.
Niko Stolwijk
What you want is
(id, name) | id | name
As this is ambiguous content (when the parser encounters id it cannot
decide if it should follow the (id, name) or the id model) you need to
write it in a non ambiguous way (XML Schema does not accept ambiguos
content) and that can be done as follows:

(id, name?) | name

That means either id optionally followed by name or name.

In XML Schema notation that will be:
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element ref="id"/>
<xs:element minOccurs="0" ref="name"/>
<xs:sequence>
<xs:element ref="name"/>
</xs:choice>
</xs:complexType>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Mar 14 '06 #5
George's solution works fine !
I have to study a bit more :-)

Mar 14 '06 #6
If you don't care about the order id and name are listed you could use
the following:

<xs:complexType>
<xs:choice minOccurs="1" maxOccurs="2">.
<xs:element ref="id"/>
<xs:element ref="name"/>
</xs:choice>
</xs:complexType>

This at least prevents empty content. Unfortunately it would allow for
id followed by id, or name followed by name.

-Jammy

Mar 15 '06 #7

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

Similar topics

1
by: Xaradas | last post by:
Someone could tell me how can I validate an xml file against an Xschema using php? Thanks and sorry for my little english. ------------------------------------- "Computer Science is no more...
3
by: Karl | last post by:
Hi! I'm having trouble with referencing different schemas. I'm not sure how it's supposed to work. When I work on the files, the targetNamespace="www.test.com/schemas/test.xsd" does not exist...
4
by: Binesh Bannerjee | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi. In another thread, Martin Honnen <mahotrash@yahoo.de> wrote: > XHTML is XML so there is no problem to use it inside of an XML document e.g. > ...
117
by: Peter Olcott | last post by:
www.halting-problem.com
0
by: Tjerk Wolterink | last post by:
I want to define an schema that allows the following documents: The name of the elements do not matter, but all the elements should have the following attribute: <xsd:attribute name="type"...
9
by: Tjerk Wolterink | last post by:
I have an xml document that conforms to my xschema document. Now i wanted to use xinclude in my xml document. But when i want to validate the xml document to the xschema i get the following...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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.