Strict order of elements being a pain

C

Christine McGavran

To continue a previous thread, sort of...

I have defined a schema for describing a windows-style user interface. My
application correctly parses and uses that schema. I'm now trying to get
that schema to be legal, so that if I threw my UI.xml and UISchema.xml at a
validator it would succeed.

I have just learned that the order of elements is important, that is if you
define a schema with element a followed by element b, then in the xml they
must also be in that order. That turns out to be very inconvenient for me,
as the order is important for my UI system. I'm curious why this order is
seen as an important thing, and again looking for suggestions on how to do
things more legally in my situation.

A rough example:

<complexType name="Window">
<sequence>
<element name="align" type="ui:Align" minOccurs="0" maxOccurs="1"
/>
<element name="size" type=ui:Size" minOccurs="0" maxOccurs="1" />
<element name="children" minOccurs="0" maxOccurs="1">
<complexType>
<sequence>
<element name="redWindow" type="ui:Window"
minOccurs="0" maxOccurs="unbounded" />
<element name="blueWindow" type="ui:Window"
minOccurs="0" maxOccurs="unbounded" />
</sequence>
</complexType>
</element>
</sequence>
</complexType>
<element name="window" type="ui:Window" />


I would like my xml to be able to create a window with children windows from
left to right being red blue red. For example:

<window>
<size x="30" y="10" />
<children>
<redWindow>
<align left="parentLeft" top="parentTop" bottom="parentBottom"
/>
<size x="10" />
</redWindow>
<blueWindow>
<align left="prevRight" top="parentTop" bottom="parentBottom" />
<size x="10" />
</blueWindow>
<redWindow>
<align left="prevRight" top="parentTop" bottom="parentBottom" />
<size x="10" />
</redWindow>
</children>
</window>

This xml will not validate because redWindow is not allowed to follow
blueWindow. My application, however, parses and uses this xml just fine. You
might be able to think of a few ways around this simple case, but when the
window contents get complex, being able to enforce order becomes very handy.
The only other alternative I can think of is to put some GUID on each window
to use for ID in alignment, which seems like a pain for the user.

Thanks in advance,
Christine
 
M

Martin Honnen

Christine McGavran wrote:

I have defined a schema for describing a windows-style user interface. My
application correctly parses and uses that schema. I'm now trying to get
that schema to be legal, so that if I threw my UI.xml and UISchema.xml at a
validator it would succeed.

I have just learned that the order of elements is important, that is if you
define a schema with element a followed by element b, then in the xml they
must also be in that order. That turns out to be very inconvenient for me,
as the order is important for my UI system. I'm curious why this order is
seen as an important thing, and again looking for suggestions on how to do
things more legally in my situation.

Nobody forces you to define a schema at all to use XML, and nobody
forces you to use a sequence in a type definition. There are other ways
to define a complex type than using a sequence, for instance choice or
all. Thus if the order of elements doesn't matter to you then consider a
different type definition.
 
C

Christine McGavran

Nobody forces you to define a schema at all to use XML

No, but I would like to be able to for a few reasons. One is customers are
given a formal documentation of our scripting syntax, and a way to test if
their scripts are not valid. Another is the DevStudio xml editor behaves
nicely if you have a schema.
There are other ways
to define a complex type than using a sequence, for instance choice or
all. Thus if the order of elements doesn't matter to you then consider a
different type definition.

Choice requires that only one of the elements occur. That doesn't work, as
it would not allow me to have both a redWindow and a blueWindow as a child.
All requires that no element may occur more than once, which means I could
have redWindow and blueWindow in either order, but I could not have two
redWindows. Both of those restrictions are unacceptable in my situation. Or
am I missing something?
 
O

Oliver Bonten

Christine,

Choice requires that only one of the elements occur. That doesn't work, as
it would not allow me to have both a redWindow and a blueWindow as a child.
All requires that no element may occur more than once, which means I could
have redWindow and blueWindow in either order, but I could not have two
redWindows. Both of those restrictions are unacceptable in my situation. Or
am I missing something?

yes, you are missing something. You can give a repetition count for the
Choice group. I don't know how to say this in XML Schema, but in a DTD it
would be (redWindow|blueWindow)+ (as opposed to (redWindow|blueWindow),
which is just the Choice), and this construction can be expressed in XML
Schema as well.

Oliver
 
M

Martin Honnen

Christine McGavran wrote:

Choice requires that only one of the elements occur. That doesn't work, as
it would not allow me to have both a redWindow and a blueWindow as a child.
All requires that no element may occur more than once, which means I could
have redWindow and blueWindow in either order, but I could not have two
redWindows. Both of those restrictions are unacceptable in my situation. Or
am I missing something?

Yes, you are missing that you can give min and/or maxOccurs attributes
to schema elements, that you can nest a sequence in a choice etc. That
gives you a lot of ways to express how the content should look.
For instance to allow any number of blueWindow and redWindow child
elements in any order you could use a schema alike

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

<xs:element name="windows">
<xs:complexType>
<xs:sequence>
<xs:element ref="window" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="window">
<xs:complexType>
<xs:sequence>
<xs:element name="children">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="redWindow" />
<xs:element name="blueWindow" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

As the type of the <children> element is a choice of redWindow or
blueWindow that can occur zero to unbounded times you can put in any
combination of <redWindow> and <blueWindow> child elements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top