Re: Conditional processing im Schema!

Hi San,

> Hello,
> I am working with xml schema.I have a problem.
> I want to generate conditional processing, for example
> when this element has this worth, then another element is permitted to
> appear!
> Is that possible??
>
> An example of my problem:
>
> Schema:
>
> <xsd:schema xmlns:sch="http://www.ascc.net/xml/Schematron"
> xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
> elementFormDefault="qualified">
>         <xsd:element name="moi">
> <xsd:complexType>
>                 <xsd:sequence>
>                  <xsd:element ref="A"/>
>                  <xsd:element ref="B"/>
>                  <xsd:element name="C" type="CType"/>
>                 </xsd:sequence>
>          </xsd:complexType>
>          </xsd:element>
>          <xsd:element name="A" type="xsd:integer"/>
>          <xsd:element name="B" type="xsd:integer"/>
>          <xsd:complexType name="CType">
>            <xsd:sequence>
>              <xsd:element name="D" type="string"
> minOccurs="0" maxOccurs="unbounded"/>
>              <xsd:element name="E" type="xsd:integer"/>
>            </xsd:sequence>
>          </xsd:complexType>
> </xsd:schema>
>
> A XML-Instance:
>
> <moi xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-
> instance" xsi:noNamespaceSchemaLocation="C:\Dokumente und
> Einstellungen\sc.ESCHER\Eigene Dateien\essai.xsd">
>         <A>30</A>
>         <B>40</B>
>         <C>
>           <D>Hello</D>
>           <E>20</E>
>         </C>
> </moi>
>
> Now, I want to generate, if A=10, then element D in C does
> not appear!

Unfortunately you can't do this with version 1.0 of W3C XML Schema. I think
conditional processing are on the list for support in a future version.
However, until that happens you have to come up with a better solution. Since
you have included a namespace for Schematron (note that schematron should be
spelled with a lower case 's' in the namespace) in your XML Schema above your
on the right track to one solution. When I have constaints like this I usually
include a Schematron rule to handle the constraints that can't be validated
with W3C XML Schema.
In your case your XML Schema could be:

<xsd:schema xmlns:sch="http://www.ascc.net/xml/schematron"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xsd:element name="moi">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element ref="A"/>
    <xsd:element ref="B"/>
    <xsd:element name="C" type="CType"/>
   </xsd:sequence>
  </xsd:complexType>
 </xsd:element>
 <xsd:element name="A" type="xsd:integer"/>
 <xsd:element name="B" type="xsd:integer"/>
 <xsd:complexType name="CType">
  <xsd:sequence>
   <xsd:element name="D" type="xsd:string" minOccurs="0" maxOccurs="unbounded">

    <xsd:annotation>
     <xsd:appinfo>
      <sch:pattern name="Condition for existance">
       <sch:rule context="C/D">
        <sch:assert test="/moi/A = 10">If a D element is present the value of
/moi/A must the 10</sch:assert>
       </sch:rule>
      </sch:pattern>
     </xsd:appinfo>
    </xsd:annotation>
   </xsd:element>
   <xsd:element name="E" type="xsd:integer"/>
  </xsd:sequence>
 </xsd:complexType>
</xsd:schema>

You should also change your namespace for XML Schema from the old CR version to
the Rec version as in my schema above.
The Schematron Validator from Topologi allows you to validate embedded
schematron rules as part of XML Schema validation and is available from [1].
For more information about extending XML Schema see [2].

Cheers,
/Eddie

[1] www.topologi.com
[2] http://www.xfront.com/BestPracticesHomepage.html

Received on Thursday, 30 August 2001 20:27:26 UTC