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

Re: HELP: schema validation error

From: Eddie Robertsson <erobertsson@allette.com.au>
Date: Fri, 09 Aug 2002 17:38:36 +1000
Message-ID: <3D53717C.5060609@allette.com.au>
To: ajerbi@mmm.com
CC: xmlschema-dev@w3.org

Hi Akram,

>Here is the schema I am using:
><!--
>     Define the Request element
>  -->
>  <xs:element name="request">
>     <xs:complexType>
>       <xs:sequence>
>          <xs:group ref="requestGroup"/>
>          <xs:element ref="field" minOccurs="0" maxOccurs="unbounded"/>
>       </xs:sequence>
>     </xs:complexType>
>  </xs:element>
>  <!--
>     Define the request group
>  -->
>  <xs:group name="requestGroup">
>     <xs:all>
>       <xs:element name="facilityName" type="xs:string"/>
>       <xs:element name="printerName" type="xs:string" />
>       <xs:element name="copies" type="xs:positiveInteger" default="1"/>
>
>     </xs:all>
>  </xs:group>
>
</snip>

>Here is the error message I get when I parse an xml file that uses the
>above schema with the following parser: org.apache.xerces.parsers.SAXParser
>Error:  cos-all-limited.1.2: A group whose content is "all" must only
>appear as the content type of a complex type definition.  Saw group in
>"sequence".
>I don't get this error when I use a different parser.
>
Then the other parsers are wrong. Xerces is correct in giving the error 
message and the message explains what the problem is. When a W3C XML 
Schema processor sees

  <xs:sequence>
    <xs:group ref="requestGroup"/>
    <xs:element ref="field" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>

it will dereference the group ref and hence create the following content model:

  <xs:sequence>
    <xs:all>
      <xs:element name="facilityName" type="xs:string"/>
      <xs:element name="printerName" type="xs:string" />
      <xs:element name="copies" type="xs:positiveInteger" default="1"/>
    </xs:all>
    <xs:element ref="field" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>

This is not a legal content model because the <xs:all> group can only appear as the single child and cannot be nested within other groups.

>My goal is to write a schema that will allow the 'request' element to have
>the first three elements ('facilityName','printerName','copies') in any
>order possible followed by any number of 'field' elements.
>
>Is it possible to write such a schema? 
>
You can't get exactly this by using W3C XML Schema alone. However, you 
can come fairly close by wrapping the three elements that can come in 
any order in an extra element like this:

<xs:element name="request">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="requestGroup">
        <xs:complexType>
          <xs:all>
            <xs:element name="facilityName" type="xs:string"/>
            <xs:element name="printerName" type="xs:string" />
            <xs:element name="copies" type="xs:positiveInteger" default="1"/>
          </xs:all>
        </xs:complexType>
      </xs:element>
      <xs:element ref="field" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

You can get exactly what you want if you don't mind using embedded Schematron rules in your W3C XML Schema but this requires a bit more processing. See [1] for more information.

Cheers,
/Eddie

[1] http://www.topologi.com/public/Schtrn_XSD/Paper.html
Received on Friday, 9 August 2002 03:23:19 GMT

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