473,386 Members | 1,733 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,386 software developers and data experts.

XML Schema - Element Order

Hi,

I have read various articles on this and I have had a few suggestions
but would like to know which is correct.

I have an XML file with the element <root>

Within <root> I can have a number of child elements occuring in any
order and any number of times.

eg ....

XML File

<root>
<a/>
<a/>
<c/>
<a/>
<b/>
</root>

I have been reading it is the way I declare the <xs:choice> element so
have thought of this schema below. This should allow me to use element
a, b, or c in any order and as many times as possible.

Schema File

<xs:element name="root">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
<xs:element ref="c"/>
</xs:choice>
</xs:complexType>
</xs:element>

Does this snippet of my schema do what I want correctly or does it do
something different?

Any advice on this would be greatly appreciated

Jonathan Tremlett

Mar 15 '06 #1
5 3396
Yes, it does exactly what you described.

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

Mar 15 '06 #2
I have a similar situation as Jonathan, but with one more requirement.
I would like to be able to restrict how many times some of the elements
appear.

I was hoping I could write a schema like the following where element
'a' is restricted to maxOccur of 3:

<xs:element name="root">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="a" maxOccurs="3"/>
<xs:element name="b"/>
<xs:element name="c"/>
</xs:choice>
</xs:complexType>
</xs:element>

This element level restriction doesn't seem to work... at least with
..Net's schema validation.

Any ideas?

Thanks,
Jammy

Mar 15 '06 #3
The simplest thing in this case is to use Schematron embedded rules to
check the occurences of a. A working schema that does this is below:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="root">
<xs:annotation>
<xs:appinfo>
<pattern xmlns="http://www.ascc.net/xml/schematron" name="Check
number of occurances">
<rule context="root">
<assert test="count(a)&lt;=3">There should maximum 3
occurences of a in root.</assert>
</rule>
</pattern>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="a"/>
<xs:element name="b"/>
<xs:element name="c"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

For your example one can write a complex content model to hanlde what
you want like below:

(b|c)*,(a,(b|c)*, (a,(b|c)*, (a, (b|c)*)?)?)?

that in XML Schema notation can be written as

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:group ref="others" minOccurs="0" maxOccurs="unbounded"/>
<xs:sequence minOccurs="0">
<xs:element name="a"/>
<xs:group ref="others" minOccurs="0" maxOccurs="unbounded"/>
<xs:sequence minOccurs="0">
<xs:element name="a"/>
<xs:group ref="others" minOccurs="0"
maxOccurs="unbounded"/>
<xs:sequence minOccurs="0">
<xs:element name="a"/>
<xs:group ref="others" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:sequence>
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:group name="others">
<xs:choice>
<xs:element name="b"/>
<xs:element name="c"/>
</xs:choice>
</xs:group>
</xs:schema>

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

Mar 15 '06 #4
After I thought about this I understand why this doesn't work. The
minOccurs/maxOccurs on xs:choice is how many choices are allowed. So
'a' can be selected multiple times and each time with a maxOccurs of 3.

Still, if anyone has a suggestion how I can have an unordered list
where every item can appear 0/1 times and some items more.... please
let me know.

Thanks,
Jammy

Mar 15 '06 #5
Remember, Schema is *NOT* intended to cover every possible constraint on
the data, just a useful common set of them. It is still, and always,
your application code's responsibility to impose any checks that Schema
can't handle.

Think of schema more as a first stage than as a complete solution.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Mar 15 '06 #6

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

Similar topics

2
by: Mark Constant | last post by:
All I want to do is have a xml file like this <Entertainment> <Movie> <Title>(Title Here)</Title> <Rating>(Rating Here)</Title> </Movie> <Movie> <Title>(Title Here)</Title> <Rating>(Rating...
1
by: mangesh | last post by:
Hi, In the following XML, I need to validate the values provided in Order tag using schema. <?xml version="1.0" encoding="UTF-8"?> <Control...
4
by: Iain A. Mcleod | last post by:
Hi I'm stuck with the following schema validation problem in VS.NET 2003: I have two types of xml document and related schema: project and projectCollection. A projectcollection is just a set...
0
by: Deep Purple | last post by:
I started with the following error... ------------------------------------------------------- An error occured while loading the schema with TargetNamespace 'http://www.w3.org/2001/XMLSchema'...
1
by: billa1972 | last post by:
Hi, I am trying to hook into Yellow Freight's rating webservice. Below is the wsdl. When i try and create a proxy file with wsdl.exe i get the following errors, see below. Also, when i...
2
by: MaLec | last post by:
I’d like to use the DataSet as the container for the XML files conforming to some schema. The problem is, that the operation of reading the XML file into the DataSet having its schema and then...
3
by: Chris Lieb | last post by:
I am new to XML Schema and am running into a bit of a snag. I have defined an XML-based scripting language for an updater program that I am working on. I would like to make a schema for this...
2
by: shumaker | last post by:
I designed a schema in VS C# 2005 Express and am wondering why it chooses to place a sequence tag around the elements of a table. And additionally, if I move the elements around within the table,...
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: 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
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: 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
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
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.