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

M

MENTAT

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
 
M

Martin Honnen

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

chaitanyag

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

Stan Kitsis [MSFT]

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

MENTAT

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

MENTAT

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. :)
 
S

Stan Kitsis [MSFT]

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

MENTAT

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.
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top