Re: min/maxOccurs, can be expressed in a schema?

Hi Joan,

> One question, this type of restrictions can be done with schemas?
> Or must be done in a post-schema validation?
> For example if I declare two attributes named min and max, can I control that
> max<=min with a schema?

Unfortunately you can't do these conditional relationships with the current
version of W3C XML Schema but it probably will be supported in the next version.
However, in the mean time we have to use other means to achieve these kinds of
constraints and one way to do it is to combine Schematron with W3C XML Schema.
What you do is to include a Schematron rule in the xs:appinfo element of your XML
Schema and for your constraint above it could look like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
 <xs:element name="Test">
  <xs:annotation>
   <xs:appinfo>
    <pattern name="min less than or equal to max"
xmlns="http://www.ascc.net/xml/schematron">
     <rule context="Test">
      <assert test="@min &lt;= @max">The min attribute must be less than or equal
to the max attribute.</assert>
     </rule>
    </pattern>
   </xs:appinfo>
  </xs:annotation>
  <xs:complexType>
   <xs:attribute name="min" type="xs:integer" use="required"/>
   <xs:attribute name="max" type="xs:integer" use="required"/>
  </xs:complexType>
 </xs:element>
</xs:schema>

The Schematron Validator from Topologi [1] can validate XML Schemas and
Schematron as well as provide validation results from embedded Schematron rules
like the above example. For more information about Schematron see [2] and more
information about how to extend XML Schemas can be found at [3].

Cheers,
/Eddie

[1] www.topologi.com
[2] http://www.ascc.net/xml/resource/schematron/
[3] http://www.xfront.com/BestPracticesHomepage.html (see Extending XML Schemas)

Received on Tuesday, 6 November 2001 17:45:00 UTC