XSD question: Allowing one required element and many optional elements.

Discussion in 'XML' started by MENTAT, Mar 21, 2005.

  1. MENTAT

    MENTAT Guest

    Hi Guys,

    Newbie question here. Part of my xml looks like this.

    <node>
    <description/>
    <config/>
    <log/>
    <transition/>
    <node>

    The transition element inside the node is compulsory. The other
    elements are optional and can occur as many times (minOccurs="0"
    maxOccurs="unbounded").

    How do I write an xsd that validates this?

    I tried using the choice tag with no success

    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element ref="description"/>
    <xs:element ref="config" />
    <xs:element ref="log" />
    </xs:choice>
    <xs:element ref="transition" minOccurs="1"/>

    This apparently is not valid xsd. I can't wrap the transition in
    another choice or sequence or all.

    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element ref="description"/>
    <xs:element ref="config" />
    <xs:element ref="log" />
    <xs:element ref="transition" minOccurs="1"/>
    </xs:choice>

    This seems to be a valid xsd but doesn't actually validate the xml
    properly. That is, even if the transition element is missing in the
    xml, the xsd says its valid.

    Tried using the <all> tag as well

    <xs:all>
    <xs:element ref="description" minOccurs="0" />
    <xs:element ref="log" minOccurs="0" />
    <xs:element ref="config" minOccurs="0" />
    <xs:element ref="transition" minOccurs="1"/>
    </xs:all>

    The problem with this is that I can only define the non transition
    elements only once. I cannot put the maxOccurs="unbounded" because it
    invalidates the xsd.

    I tried all the various combinations of all the above (putting
    maxoccurs in the all tag etc etc.)... No go!

    It seems to me that there should be an easy solution to this, some
    combination that works.


    Any help would be much appreciated
     
    MENTAT, Mar 21, 2005
    #1
    1. Advertising

  2. Re: XSD question: Allowing one required element and many optionalelements.

    MENTAT wrote:


    > Newbie question here. Part of my xml looks like this.
    >
    > <node>
    > <description/>
    > <config/>
    > <log/>
    > <transition/>
    > <node>
    >
    > The transition element inside the node is compulsory. The other
    > elements are optional and can occur as many times (minOccurs="0"
    > maxOccurs="unbounded").
    >
    > How do I write an xsd that validates this?


    How about xs:sequence e.g.

    <xs:element name="node">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="description" minOccurs="0"
    maxOccurs="unbounded" />
    <xs:element name="config" minOccurs="0" maxOccurs="unbounded" />
    <xs:element name="log" minOccurs="0" maxOccurs="unbounded" />
    <xs:element name="transition" minOccurs="1"
    maxOccurs="unbounded" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    that way you have the constraints modelled as described. Of course the
    order of elements is constrained that way too but your description
    doesn't make it clear whether you do not want that.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 21, 2005
    #2
    1. Advertising

  3. MENTAT

    MENTAT Guest

    Yeah, this seems to work, but I don't want to constrain the order of the elements.

    > How about xs:sequence e.g.
    >
    > <xs:element name="node">
    > <xs:complexType>
    > <xs:sequence>
    > <xs:element name="description" minOccurs="0"
    > maxOccurs="unbounded" />
    > <xs:element name="config" minOccurs="0" maxOccurs="unbounded" />
    > <xs:element name="log" minOccurs="0" maxOccurs="unbounded" />
    > <xs:element name="transition" minOccurs="1"
    > maxOccurs="unbounded" />
    > </xs:sequence>
    > </xs:complexType>
    > </xs:element>
    >
    > that way you have the constraints modelled as described. Of course the
    > order of elements is constrained that way too but your description
    > doesn't make it clear whether you do not want that.
     
    MENTAT, Mar 22, 2005
    #3
  4. MENTAT

    Guest

    A more challenging aspect of the problem:

    Lets modify the xml slightly

    <root>
    <dialog>
    <node>
    <description/>
    <config/>
    <log/>
    <transition/>
    <node>
    <transition/>
    <dialog>
    <transition/>
    <\root>

    I need a rule that says that its ok if a node doesn't have a transition
    as long as the dialog or root has a transition (it is possible for all
    3 to have a transition, which is ok). Note that a root can contain many
    dialogs and a dialog can contain many nodes.

    What i need is some sort of fall through/ sibling navigation, parent
    navigation sort of command. How do I go about doing this in XSD?
     
    , Mar 22, 2005
    #4
  5. The following snippet lets you have description,config, and log in any order
    any number of times. The only restriction it presents is that transition
    element has to be at the end.

    <xs:sequence>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element ref="description"/>
    <xs:element ref="config" />
    <xs:element ref="log" />
    </xs:choice>
    <xs:element ref="transition" minOccurs="1"/>
    </xs:sequence>
    --
    Stan Kitsis
    Program Manager, XML Technologies
    Microsoft Corporation

    This posting is provided "AS IS" with no warranties, and confers no rights.


    "MENTAT" <> wrote in message
    news:...
    > Yeah, this seems to work, but I don't want to constrain the order of the
    > elements.
    >
    >> How about xs:sequence e.g.
    >>
    >> <xs:element name="node">
    >> <xs:complexType>
    >> <xs:sequence>
    >> <xs:element name="description" minOccurs="0"
    >> maxOccurs="unbounded" />
    >> <xs:element name="config" minOccurs="0" maxOccurs="unbounded" />
    >> <xs:element name="log" minOccurs="0" maxOccurs="unbounded" />
    >> <xs:element name="transition" minOccurs="1"
    >> maxOccurs="unbounded" />
    >> </xs:sequence>
    >> </xs:complexType>
    >> </xs:element>
    >>
    >> that way you have the constraints modelled as described. Of course the
    >> order of elements is constrained that way too but your description
    >> doesn't make it clear whether you do not want that.
     
    Stan Kitsis [MSFT], Mar 25, 2005
    #5
  6. MENTAT

    MENTAT Guest

    Thanks Stan.
    I realised that I wouldn't be able to get it working the way I wanted
    and would have to make compromises and put in restrictions. In the end
    I changed the specs around so that I could use
    <xs:all>
    <xs:element ref="description" minOccurs="0"/>
    <xs:element ref="config" minOccurs="0"/>
    <xs:element ref="log" minOccurs="0"/>
    <xs:element ref="transition" minOccurs="1"/>
    <xs:all>

    This way elements can occur in any order and transition is compulsory,
    but it means that each element can occur only once. This is something
    I just have to live with now.

    Thanks once again Stan. How about my question in the other branch of
    this thread, regarding the "fall-through" of transitions. Thats the
    one driving me up the wall at the moment. Any hints/helps would be
    helpful.




    "Stan Kitsis [MSFT]" <> wrote in message news:<4244576b$>...
    > The following snippet lets you have description,config, and log in any order
    > any number of times. The only restriction it presents is that transition
    > element has to be at the end.
    >
    > <xs:sequence>
    > <xs:choice minOccurs="0" maxOccurs="unbounded">
    > <xs:element ref="description"/>
    > <xs:element ref="config" />
    > <xs:element ref="log" />
    > </xs:choice>
    > <xs:element ref="transition" minOccurs="1"/>
    > </xs:sequence>
    > --
    > Stan Kitsis
    > Program Manager, XML Technologies
    > Microsoft Corporation
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    > "MENTAT" <> wrote in message
    > news:...
    > > Yeah, this seems to work, but I don't want to constrain the order of the
    > > elements.
    > >
    > >> How about xs:sequence e.g.
    > >>
    > >> <xs:element name="node">
    > >> <xs:complexType>
    > >> <xs:sequence>
    > >> <xs:element name="description" minOccurs="0"
    > >> maxOccurs="unbounded" />
    > >> <xs:element name="config" minOccurs="0" maxOccurs="unbounded" />
    > >> <xs:element name="log" minOccurs="0" maxOccurs="unbounded" />
    > >> <xs:element name="transition" minOccurs="1"
    > >> maxOccurs="unbounded" />
    > >> </xs:sequence>
    > >> </xs:complexType>
    > >> </xs:element>
    > >>
    > >> that way you have the constraints modelled as described. Of course the
    > >> order of elements is constrained that way too but your description
    > >> doesn't make it clear whether you do not want that.
     
    MENTAT, Mar 28, 2005
    #6
  7. MENTAT

    MENTAT Guest

    I take it from the silence of the newsgroup (and my own frustrated
    attempts at finding a solution), that the problem below doesn't
    actually have a solution?

    Any confirmation would be appreciated as well and would help me get
    "closure" on this issue. :)


    wrote in message news:<>...
    > A more challenging aspect of the problem:
    >
    > Lets modify the xml slightly
    >
    > <root>
    > <dialog>
    > <node>
    > <description/>
    > <config/>
    > <log/>
    > <transition/>
    > <node>
    > <transition/>
    > <dialog>
    > <transition/>
    > <\root>
    >
    > I need a rule that says that its ok if a node doesn't have a transition
    > as long as the dialog or root has a transition (it is possible for all
    > 3 to have a transition, which is ok). Note that a root can contain many
    > dialogs and a dialog can contain many nodes.
    >
    > What i need is some sort of fall through/ sibling navigation, parent
    > navigation sort of command. How do I go about doing this in XSD?
     
    MENTAT, Mar 28, 2005
    #7
  8. You can't do conditions like this in XML Schema. You can try using a big
    choice with all possible states

    --
    Stan Kitsis
    Program Manager, XML Technologies
    Microsoft Corporation

    This posting is provided "AS IS" with no warranties, and confers no rights.


    <> wrote in message
    news:...
    >A more challenging aspect of the problem:
    >
    > Lets modify the xml slightly
    >
    > <root>
    > <dialog>
    > <node>
    > <description/>
    > <config/>
    > <log/>
    > <transition/>
    > <node>
    > <transition/>
    > <dialog>
    > <transition/>
    > <\root>
    >
    > I need a rule that says that its ok if a node doesn't have a transition
    > as long as the dialog or root has a transition (it is possible for all
    > 3 to have a transition, which is ok). Note that a root can contain many
    > dialogs and a dialog can contain many nodes.
    >
    > What i need is some sort of fall through/ sibling navigation, parent
    > navigation sort of command. How do I go about doing this in XSD?
    >
     
    Stan Kitsis [MSFT], Apr 1, 2005
    #8
  9. MENTAT

    MENTAT Guest

    I tried doing a choice between all the possible combinations. Doesn't
    work. Because at some stage I have to make a choice between the
    element node(or dialog) with one data type and the element node (or
    dialog) with another data type and XSD doesn't allow that.

    Basically somewhere I would have to go

    <choice>
    <element name="dialog" type="optional_transition"/>
    <element name="dialog" type="compulsory_transition"/>
    </choice>

    This doesn't work because the element name is the same. It can only
    choose different elements, not different element types. Same goes for
    <all> and all the other tags. There seems to be a choice for elements
    themselves but not the element types.

    Hmmmm....Bit of a non trivial restriction isn't it. Anyway, at least
    now I know its not something i am overlooking.

    Thanks again Stan.



    "Stan Kitsis [MSFT]" <> wrote in message news:<424da294$>...
    > You can't do conditions like this in XML Schema. You can try using a big
    > choice with all possible states
    >
    > --
    > Stan Kitsis
    > Program Manager, XML Technologies
    > Microsoft Corporation
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    > <> wrote in message
    > news:...
    > >A more challenging aspect of the problem:
    > >
    > > Lets modify the xml slightly
    > >
    > > <root>
    > > <dialog>
    > > <node>
    > > <description/>
    > > <config/>
    > > <log/>
    > > <transition/>
    > > <node>
    > > <transition/>
    > > <dialog>
    > > <transition/>
    > > <\root>
    > >
    > > I need a rule that says that its ok if a node doesn't have a transition
    > > as long as the dialog or root has a transition (it is possible for all
    > > 3 to have a transition, which is ok). Note that a root can contain many
    > > dialogs and a dialog can contain many nodes.
    > >
    > > What i need is some sort of fall through/ sibling navigation, parent
    > > navigation sort of command. How do I go about doing this in XSD?
    > >
     
    MENTAT, Apr 4, 2005
    #9
    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. ANoobee
    Replies:
    3
    Views:
    394
  2. Thomas Wittek
    Replies:
    4
    Views:
    1,503
    Joe Kesselman
    Sep 13, 2007
  3. Philipp
    Replies:
    2
    Views:
    535
    Philipp
    Apr 11, 2008
  4. Spaulding

    Required and optional elements

    Spaulding, Aug 14, 2009, in forum: XML
    Replies:
    1
    Views:
    943
    Martin Honnen
    Aug 15, 2009
  5. Terry Murray

    Allowing for one and only one print job

    Terry Murray, Nov 5, 2003, in forum: Javascript
    Replies:
    2
    Views:
    86
    kaeli
    Nov 5, 2003
Loading...

Share This Page