Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   XSD question: Allowing one required element and many optional elements. (http://www.velocityreviews.com/forums/t169015-xsd-question-allowing-one-required-element-and-many-optional-elements.html)

MENTAT 03-21-2005 08:33 AM

XSD question: Allowing one required element and many optional elements.
 
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

Martin Honnen 03-21-2005 04:16 PM

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/

MENTAT 03-22-2005 05:07 AM

Re: XSD question: Allowing one required element and many optional elements.
 
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.


chaitanyag@hotmail.com 03-22-2005 05:39 AM

Re: XSD question: Allowing one required element and many optional elements.
 
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] 03-25-2005 06:24 PM

Re: XSD question: Allowing one required element and many optional elements.
 
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" <chaitanyag@hotmail.com> wrote in message
news:5a286c02.0503212107.24baa2d7@posting.google.c 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.




MENTAT 03-28-2005 10:41 PM

Re: XSD question: Allowing one required element and many optional elements.
 
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]" <skits@microsoft.com> wrote in message news:<4244576b$1@news.microsoft.com>...
> 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" <chaitanyag@hotmail.com> wrote in message
> news:5a286c02.0503212107.24baa2d7@posting.google.c 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.


MENTAT 03-28-2005 10:48 PM

Re: XSD question: Allowing one required element and many optional elements.
 
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. :-)


chaitanyag@hotmail.com wrote in message news:<1111469996.196709.56910@z14g2000cwz.googlegr 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?


Stan Kitsis [MSFT] 04-01-2005 07:32 PM

Re: XSD question: Allowing one required element and many optional elements.
 
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.


<chaitanyag@hotmail.com> wrote in message
news:1111469996.196709.56910@z14g2000cwz.googlegro 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?
>




MENTAT 04-04-2005 04:16 AM

Re: XSD question: Allowing one required element and many optional elements.
 
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]" <skits@microsoft.com> wrote in message news:<424da294$1@news.microsoft.com>...
> 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.
>
>
> <chaitanyag@hotmail.com> wrote in message
> news:1111469996.196709.56910@z14g2000cwz.googlegro 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?
> >



All times are GMT. The time now is 01:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.