Re: HELP: schema validation error

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 UTC