473,385 Members | 1,655 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: restrict number of elements with certain attribute value

Hi there.

I have a question regarding restrictions in an XML Schema definition.

My XML contains a structure like this:

<fruit-bowl>
<fruit name="apple" />
<fruit name="pear" />
<fruit name="lychee" favourite="true" />
</fruit-bowl>

The problem is that it should be impossible to specify more than one
favourite. However, I don't know how to write this rule in XSD. I need
to restrict the number of elements *with a certain attribute value*.
Any ideas?

Everything else is there ... this is as far as I've got:

<xs:complexType name="FruitBowlType">
<xs:sequence>
<xs:element name="module" type="FruitType" minOccurs="1"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="FruitType">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="favourite" type="boolean" />
</xs:complexType>

May 10 '07 #1
4 3775
Joshua Mostafa wrote:
The problem is that it should be impossible to specify more than one
favourite.
XML Schemas doesn't express this kind of ... ahem ... co-dependency. I
would suggest you change your document structure to express the idea in
a way that schemas can handle. For example:

<fruit-bowl favourite="lychee">
<fruit name="apple" />
<fruit name="pear" />
<fruit name="lychee"/>
</fruit-bowl>

or

<fruit-bowl>
<favorite-fruit name="lychee"/>
<fruit name="apple"/>
<fruit name="pear" />
</fruit-bowl>

I think you can structure either of these to guarantee consistancy.

The only other solution I can think of is to define element types for
both fruit and favorite-fruit, structure the fruit-bowl so it can only
contain one favorite-fruit, then use the xsi:type directive to force one
of the fruit elements to actually be a favorite-fruit. But I think
xsi:type can only select among subclasses, which would mean a fruit with
that override would also be an instance of fruit, which would seem to
create a conflict in terms of how to interpret it... But that's well
outside what I've actually tried to do with schemas, so don't take my
word for it; find someone who knows, or try it and see if it fails.

However, I don't know how to write this rule in XSD. I need
to restrict the number of elements *with a certain attribute value*.
Any ideas?

Everything else is there ... this is as far as I've got:

<xs:complexType name="FruitBowlType">
<xs:sequence>
<xs:element name="module" type="FruitType" minOccurs="1"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="FruitType">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="favourite" type="boolean" />
</xs:complexType>

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
May 10 '07 #2
Joshua Mostafa <mi*****@gmail.comwrote in
<11**********************@n59g2000hsh.googlegroups .com>:
I have a question regarding restrictions in an XML Schema
definition.
[...]
The problem is that it should be impossible to specify
more than one favourite. However, I don't know how to
write this rule in XSD. I need to restrict the number of
elements *with a certain attribute value*. Any ideas?
It seems to me that the XML community is coming to the
consensus that W3C's XML Schemata are only to be used for
checking the structural validity of XML documents, and not
for checking the logical validity. (Well, the XML Schema WG
members themselves were probably well-aware of what they
were doing ever since they started working on XML Schema
recommendations.)

I believe Joseph Kesselman recently quoted Dr. Chomsky as a
good example of what that means. I'll do the same:

<quote>

1. Colorless green ideas sleep furiously.
2. Furiously sleep ideas green colorless.

It is fair to assume that neither sentence (1) nor (2) (nor
indeed any part of these sentences) had ever occurred in an
English discourse. Hence, in any statistical model for
grammaticalness, these sentences will be ruled out on
identical grounds as equally "remote" from English. Yet
(1), though nonsensical, is grammatical, while (2) is not.

</quote>

Basically, XML Schemata are for weeding out (2)s, but not
(1)s. As the passage quoted indicates, the boundary between
the two is actually a bit fuzzy, and you may define
grammars that will make certain meanings outright invalid
(Joseph Kesselman's response to your posts contains
examples of those). As a result, if your documents are
highly structured, checking the structural validity will
catch many of the logical inconsistencies. If they're not
(as yours isn't), you'll have to explicitly check for
logical inconsistencies using some other means.

--
Pavel Lepin
May 10 '07 #3
Pavel Lepin wrote:
It seems to me that the XML community is coming to the
consensus that W3C's XML Schemata are only to be used for
checking the structural validity of XML documents, and not
for checking the logical validity.
I wouldn't quite say "only" -- if your concepts of logical validity are
simple enough to be expressed by the schema, you're good to go, and
sometimes restructuring the document will permit you to achieve that
even for some moderately complex concepts.

But there will be many cases where schema can't do it all, though it
will simplify your life by doing much of it... and by helping you
explain at least some of the constraints to the users (and to generic
XML tools).
(Well, the XML Schema WG
members themselves were probably well-aware of what they
were doing ever since they started working on XML Schema
recommendations.)
I believe so. XML schemas were based in large part on database schemas,
which are also about structuring the data rather than checking its
meaning. The fully general checking problem needs a fully general
programming language, which isn't what XML Schema was designed to be.

There are other schema languages and validation frameworks for XML, such
as Relax-NG or Schematron, which can express things XML Schema can't.
The downside of using these, of course, is that since they aren't part
of the W3C's core standard set they're less likely to be supported by
any given set of tools and there are fewer people who are really fluent
in them. I haven't used them yet -- I haven't done anything that needed
complicated validation, and indeed many of the documents I work with are
minimally validated or simply well-formed -- but they're certainly worth
looking at as educational material, and may be worth considering if you
have enough control over your environment to count on their being available.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
May 10 '07 #4
Thanks for all the enlightening responses! The linguistic analogy in
particular made the difference clear.

The favourite-fruit option seems to me to be the best solution; that
way I can demand at least one fruit. If I wanted to subclass, to avoid
redundancy in type definition, I could always subclass both fruit and
favourite-fruit from an abstract-fruit type.

May 11 '07 #5

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

Similar topics

0
by: Ray Tayek | last post by:
hi, trying to convert some csv files into xsml and pulling a few hairs out :(. using the files below. a java program will parse the csv and take care of strange names and notes that line breaks in...
2
by: monsterpot | last post by:
Hello, I have a document like this: <parameters> <param ptype="init">somedata</param> <param ptype="process">moredata</param> <param ptype="kill">alsodata</param> </parameters> <param> has...
3
by: man-in-nature | last post by:
Hello, I have already read several existing posts about xsd:extension, but do not find something useful to my test case. I have one xml file and one xsd file. I can use a simple command line...
5
by: Paw Pedersen | last post by:
Is it possible to load a XSD and loop throw the nodes and attributes that you whant filled out, and then generate a XML instance with the trees and data that you have filled data in? And how do...
9
by: Eskimo | last post by:
I get "The attribute targetNamespace does not match the designated namespace URI" and nothing else. I have an xml file, that was pre-existing. I have to validate against the schema provided, I...
2
by: foolproofplan | last post by:
I am new to making schema for xml and am not exactly sure on a certain restriction i need to make within an xsd. Take for example, a complexType Car. I am trying to restrict certain elements from...
0
by: Steve Fletcher | last post by:
Hello, I'm writing a schema, and I want to apply some contraints to the combination of values that can be supplied for the attributes for one of the elements. The problem concerns the Operation...
9
by: =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= | last post by:
Hi, I have a schema that has an optional element, fieldTag4000Field. If the element is omitted from the XML request, when it is deserialized, it will be null when I check it - which is fine. ...
5
by: forest demon | last post by:
i'm trying to create a .xsd file to validate the XML below(just a small snippet). i've tried a number of things and can come close, but i can't quite get it to do what i want. i'm able to...
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: 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
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
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...

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.