Page 1 of 1

xsd:all too restrictive, what do I use?

Posted: Thu Aug 26, 2010 1:21 am
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?

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

Posted: Thu Aug 26, 2010 8:19 am
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!

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

Posted: Thu Aug 26, 2010 10:46 am
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

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

Posted: Thu Aug 26, 2010 2:23 pm
by svaens
thanks! Good suggestion.