Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSD question: Allowing one required element and many optional elements.

Reply
Thread Tools

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

 
 
MENTAT
Guest
Posts: n/a
 
      03-21-2005
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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-21-2005


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/
 
Reply With Quote
 
 
 
 
MENTAT
Guest
Posts: n/a
 
      03-22-2005
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.

 
Reply With Quote
 
chaitanyag@hotmail.com
Guest
Posts: n/a
 
      03-22-2005
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?

 
Reply With Quote
 
Stan Kitsis [MSFT]
Guest
Posts: n/a
 
      03-25-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.



 
Reply With Quote
 
MENTAT
Guest
Posts: n/a
 
      03-28-2005
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]" <(E-Mail Removed)> wrote in message news:<4244576b$(E-Mail Removed)>...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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.

 
Reply With Quote
 
MENTAT
Guest
Posts: n/a
 
      03-28-2005
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.


http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in message news:<(E-Mail Removed) oups.com>...
> 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?

 
Reply With Quote
 
Stan Kitsis [MSFT]
Guest
Posts: n/a
 
      04-01-2005
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.


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>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?
>



 
Reply With Quote
 
MENTAT
Guest
Posts: n/a
 
      04-04-2005
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]" <(E-Mail Removed)> wrote in message news:<424da294$(E-Mail Removed)>...
> 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.
>
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> >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?
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD restriction of nesting an element within another element with aspecific attribute value? PeteOlcott XML 1 02-05-2009 06:39 AM
XSD: complexType with mandatory and optional elements in arbitraryorder? Thomas Wittek XML 4 09-13-2007 12:04 PM
Required & Optional software and utilities for creating html, Javascript etc Sean HTML 8 02-15-2004 11:24 PM
Allowing for one and only one print job Terry Murray Javascript 2 11-05-2003 08:17 PM
XSD - element, element value and attribute ? Matthet XML 2 09-24-2003 04:10 PM



Advertisments