Re: Controlling what tags that can occur by using a control tag.

Hi Carl,

> I need a way to validate that an element A's special elements
> follows when someone enters 'A' as a name. And likewize that the
> element B's special elements follows when someone enters 'B' as a
> name.

This is a co-occurrence constraint. In general, you can't express
co-occurrence constraints using XML Schema, and indeed this is one of
the types that you can't. What you've tried falls foul of the unique
particle attribute rule, because when a validator comes across a name
element, it can't tell whether to choose the first or second of the
sequences that you've specified. XML Schema validators can't look
inside elements to see their structure - they only pair elements with
particles in the model based on the name of those elements.

What I'd recommend is either switching to RELAX NG, which can
represent this kind of constraint very easily, or using Schematron in
concert with your XML Schema.

The XML Schema part should permit the message to contain name elements
with the value A or B, and a variables element containing either
a_variable or b_variable:

<xs:element name="message">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="name">
        <xs:simpleType>
          <xs:restriction base="xs:token">
            <xs:enumeration value="A" />
            <xs:enumeration value="B" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="variables">
        <xs:complexType>
          <xs:choice>
            <xs:element name="a_variable" />
            <xs:element name="b_variable" />
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

The Schematron rules can then test whether the correct elements are
held within the variables element, based on the value of the name
element:

  <sch:rule context="message">
    <sch:assert test="name = 'A' and variables/a_variable">
      If the name is 'A' then the variables should contain a_variable.
    </sch:assert>
    <sch:assert test="name = 'B' and variables/a_variable">
      If the name is 'B' then the variables should contain b_variable.
    </sch:assert>
  </sch:rule>

See http://www.topologi.com/public/Schtrn_XSD/Paper.html for a
description about how to embed the Schematron into the XML Schema for
use with the Topologi Schematron Validator.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Wednesday, 27 March 2002 08:00:32 UTC