Strict order of elements being a pain

Discussion in 'XML' started by Christine McGavran, Jan 31, 2004.

  1. 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
     
    Christine McGavran, Jan 31, 2004
    #1
    1. Advertising

  2. 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.
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 31, 2004
    #2
    1. Advertising

  3. > 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?
     
    Christine McGavran, Feb 1, 2004
    #3
  4. Christine,

    On Sun, 01 Feb 2004 00:47:45 +0000, 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 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
     
    Oliver Bonten, Feb 1, 2004
    #4
  5. Christine McGavran wrote:


    >>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?


    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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 1, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olaf Petzold
    Replies:
    12
    Views:
    7,473
    Rob Dekker
    Dec 23, 2005
  2. =?Utf-8?B?S1JN?=

    DropDownList Pain

    =?Utf-8?B?S1JN?=, May 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    384
    =?Utf-8?B?S1JN?=
    May 25, 2005
  3. Daniel Manes

    Code-Behind Pain in the Behind!

    Daniel Manes, Jun 10, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    670
  4. Brent White

    Sorry to be such a pain

    Brent White, Oct 24, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    553
    Juan T. Llibre
    Oct 25, 2005
  5. Christopher R

    xhtml strict is being very stict why?

    Christopher R, Oct 1, 2003, in forum: HTML
    Replies:
    13
    Views:
    805
Loading...

Share This Page