W3C home > Mailing lists > Public > xmlschema-dev@w3.org > March 2002

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

From: Jeni Tennison <jeni@jenitennison.com>
Date: Wed, 27 Mar 2002 13:00:30 +0000
Message-ID: <79399238354.20020327130030@jenitennison.com>
To: carl.gentele@om.com
CC: "'xmlschema-dev@w3.org'" <xmlschema-dev@w3.org>
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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:14:30 GMT