xsd:all too restrictive, what do I use?

This should cover W3C XML Schema, Relax NG and DTD related problems.
svaens
Posts: 3
Joined: Thu Aug 26, 2010 1:00 am

xsd:all too restrictive, what do I use?

Post by svaens »

Hi all, and thanks in advance for any advice;

I am trying to represent some XML in an XSD schema, but having troubles.

I need to make a schema that allows for, something which was inspired from ANT, and looks like this;

Code: Select all


<when result="template.txt">
<and>
<equals property="Department" value="Finance"/>
<equals property="Size" value="1"/>
<or>
<equals property="User" value="John"/>
<equals property="User" value="Admin"/>
</or>
</and>
</when>
where the 'and', 'or', and 'not' elements must be able to accept any number of child 'and', 'or', 'not', and 'equals' elements.
There must not be any order restrictions.

That is, I think the xsd:all element almost suits the occasion, but not the cardinality requirements.

The 'when' tag is easier. It can take either 'and, equals, or, not' elements (that is the easy part, simply use an xsd:choice element.

Can anyone show me how I would define a schema which allows for the above XML?
svaens
Posts: 3
Joined: Thu Aug 26, 2010 1:00 am

Re: xsd:all too restrictive, what do I use?

Post by svaens »

Actually, having posted the question, and continued to research, I came up with what i think is the answer.

I can use a <xs:choice> element with an unbounded cardinality.

i.e., here I defined my types separately:

Code: Select all

	<xs:complexType name="SequentialLogicType">
<xs:choice maxOccurs="unbounded">
<xs:element name="equals" type="EqualsType" />
<xs:element name="and" type="SequentialLogicType" />
<xs:element name="or" type="SequentialLogicType" />
<xs:element name="not" type="SimpleLogicType" />
</xs:choice>
</xs:complexType>
<xs:complexType name="EqualsType">
<xs:attribute name="property" type="xs:string" />
<xs:attribute name="value" type="xs:string" />
</xs:complexType>
<xs:complexType name="SimpleLogicType">
<xs:choice>
<xs:element name="equals" type="EqualsType" />
<xs:element name="and" type="SequentialLogicType" />
<xs:element name="or" type="SequentialLogicType" />
<xs:element name="not" type="SimpleLogicType" />
</xs:choice>
</xs:complexType>
And it seems to be correct.

Though, feel free to correct me if I have come to the wrong understanding!
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: xsd:all too restrictive, what do I use?

Post by adrian »

Hello,

It seems to be correct. The only thing I would add is a minOccurs="2" for the xs:choice of SequentialLogicType:

Code: Select all

<xs:complexType name="SequentialLogicType">
<xs:choice minOccurs="2" maxOccurs="unbounded">
since the logical operations "and" and "or" need at least two operands to have a purpose.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
svaens
Posts: 3
Joined: Thu Aug 26, 2010 1:00 am

Re: xsd:all too restrictive, what do I use?

Post by svaens »

thanks! Good suggestion.
Post Reply