Page 1 of 1

Multiple variations of a single entity

Posted: Fri Dec 16, 2005 4:38 pm
by jsb
I'm running into trouble trying to figure out how to write a schema for validating an element that can have different appearances, depending on the value of an attribute. I'm not sure it can be done at all.

Here's an example of what we can do in our aircraft simulation model file:

<component name="Roll AP Wing Leveler" type="PURE_GAIN">
<input>fcs/attitude/sensor/phi-rad</input>
<gain>2.0</gain>
<clipto>
<min>-0.255</min>
<max>0.255</max>
</clipto>
</component>

<component name="Roll AP Error Integrator" type="INTEGRATOR">
<input> attitude/phi-rad </input>
<c1> 0.125 </c1>
</component>

<component name="Roll AP Error summer" type="SUMMER">
<input> velocities/p-rad_sec</input>
<input> fcs/roll-ap-wing-leveler</input>
<input> fcs/roll-ap-error-integrator</input>
<clipto>
<min>-1.0</min>
<max> 1.0</max>
</clipto>
</component>

<component name="Roll AP Autoswitch" type="SWITCH">
<default value="0.0"/>
<test logic="AND" value="fcs/roll-ap-error-summer">
ap/attitude_hold == 1
</test>
</component>

Each "component" element will have a name and a type attribute. Each component element can have one or more input elements. Each componentn may have zero or one output elements. THe specific content of the component will depend on which of several pre-defined types the component represents.

I tried using a choice and group structure, but that didn't quite work out. Am I out of luck?

Jon

Posted: Fri Dec 16, 2005 4:55 pm
by george
Hi Jon,

What you are trying to do is generally called co-occurrence constraint and it is not supported by the current XML Schema 1.0 language. It is on the todo list for the XML Schema 1.1 and 2.0.

Right now your alternatives are:
* Use Relax NG
* Relax the XML Schema constraint and use Schematron embedded rules to enforce the constraints
* Change the documents to use/define for instance a different element for each type value
* Use xsi:type in the instance document in the component element to specify the actual type for the content

Best Regards,
George

Posted: Sat Dec 17, 2005 3:43 am
by jsb
george wrote:Hi Jon,

What you are trying to do is generally called co-occurrence constraint and it is not supported by the current XML Schema 1.0 language. It is on the todo list for the XML Schema 1.1 and 2.0.

Right now your alternatives are:
* Use Relax NG
* Relax the XML Schema constraint and use Schematron embedded rules to enforce the constraints
* Change the documents to use/define for instance a different element for each type value
* Use xsi:type in the instance document in the component element to specify the actual type for the content

Best Regards,
George
The first two alternatives sound ... "non-standard", to me. Is that true?
I may offer the third option as the preferred alternative definition, and make the current usage obsolete, eventually.
I am most intrigued by the fourth option you present above. What is that? I don't know what you are talking about (yet)!

Jon

Posted: Mon Dec 19, 2005 1:41 pm
by george
Hi Jon,

Relax NG is a standard: ISO/IEC 19757-2 RELAX-NG
Schematron will also be an ISO standard soon.

Here it is an example of using xsi:type.

Schema file:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="components">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="component"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="component" type="componentBase"/>
<!-- -->
<xs:complexType name="componentBase">
<xs:sequence/>
<xs:attribute name="name" use="required"/>
</xs:complexType>
<!-- -->
<xs:complexType name="PURE_GAIN">
<xs:complexContent>
<xs:extension base="componentBase">
<xs:sequence>
<xs:element ref="input"/>
<xs:element ref="gain"/>
<xs:element ref="clipto"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<!-- -->
<xs:complexType name="INTEGRATOR">
<xs:complexContent>
<xs:extension base="componentBase">
<xs:sequence>
<xs:element ref="input"/>
<xs:element ref="c1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<!-- -->
<xs:complexType name="SUMMER">
<xs:complexContent>
<xs:extension base="componentBase">
<xs:sequence>
<xs:element minOccurs="3" maxOccurs="3" ref="input"/>
<xs:element minOccurs="0" ref="clipto"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<!-- -->
<xs:complexType name="SWITCH">
<xs:complexContent>
<xs:extension base="componentBase">
<xs:sequence>
<xs:sequence>
<xs:element ref="default"/>
<xs:element ref="test"/>
</xs:sequence>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<!-- -->
<xs:element name="default">
<xs:complexType>
<xs:attribute name="value" use="required" type="xs:decimal"/>
</xs:complexType>
</xs:element>
<xs:element name="test">
<xs:complexType mixed="true">
<xs:attribute name="logic" use="required" type="xs:NCName"/>
<xs:attribute name="value" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="input" type="xs:string"/>
<xs:element name="c1" type="xs:decimal"/>
<xs:element name="gain" type="xs:decimal"/>
<xs:element name="clipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="min"/>
<xs:element ref="max"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="min" type="xs:decimal"/>
<xs:element name="max" type="xs:decimal"/>
</xs:schema>
Instance document:

Code: Select all


<components xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<component name="Roll AP Wing Leveler" xsi:type="PURE_GAIN">
<input>fcs/attitude/sensor/phi-rad</input>
<gain>2.0</gain>
<clipto>
<min>-0.255</min>
<max>0.255</max>
</clipto>
</component>
<component name="Roll AP Error Integrator" xsi:type="INTEGRATOR">
<input> attitude/phi-rad </input>
<c1> 0.125 </c1>
</component>
<component name="Roll AP Error summer" xsi:type="SUMMER">
<input> velocities/p-rad_sec</input>
<input> fcs/roll-ap-wing-leveler</input>
<input> fcs/roll-ap-error-integrator</input>
<clipto>
<min>-1.0</min>
<max> 1.0</max>
</clipto>
</component>
<component name="Roll AP Autoswitch" xsi:type="SWITCH">
<default value="0.0"/>
<test logic="AND" value="fcs/roll-ap-error-summer"> ap/attitude_hold == 1</test>
</component>
</components>
Best Regards,
George