Re: Elements in group optional and repeatable

Hi Pete,

> I'd like to set up a an xs:group which permits a set of elements to
> be all optional and all repeatable. I've tried the following:
>
> [assuming I have created a named complexType for "elementType"]
[snip]
>   <xs:group name="elementsGroup">
>     <xs:choice minOccurs="0" maxOccurs="unbounded">
>       <xs:element ref="title" minOccurs="0" maxOccurs="unbounded"/>
>       <xs:element ref="creator" minOccurs="0" maxOccurs="unbounded"/>
>       <xs:element ref="subject" minOccurs="0" maxOccurs="unbounded"/>
>     </xs:choice>
>   </xs:group>
>
> XML Spy 4.3 seems to accept this (and give the desired behaviour on
> validating instances against it), but both XSV (current online
> service) and Xerces C++ 1.6.0 object to the use of
> minOccurs/maxOccurs on the xs:choice element.
>
> The error message from XSV refers to "Invalid per cvc-complex-type.1.3"
> but I'm struggling to see which rule at

It's certainly strange that XSV is indicating that as the location of
the error. The problem, I think, is that the XML representation of the
group is invalid as per "Schema Representation Constraint: Model Group
Definition Representation OK":

 "In addition to the conditions imposed on <group> element
  information items by the schema for schemas, the corresponding model
  group definition, if any, must satisfy the conditions set out in
  Constraints on Model Group Schema Components (§3.8.6)."

http://www.w3.org/TR/xmlschema-1/#section-Constraints-on-XML-Representations-of-Model-Group-Definitions

If you have a look at the schema for schema about top-level xs:group
elements, you should see the declaration:

 <xs:element name="group" type="xs:namedGroup" id="group">
  <xs:annotation>
   <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-group"/>
  </xs:annotation>
 </xs:element>

The xs:namedGroup complex type is defined as:
 
 <xs:complexType name="namedGroup">
  <xs:annotation>
   <xs:documentation>Should derive this from realGroup, but too complicated 
                      for now</xs:documentation>
  </xs:annotation>
  <xs:sequence>
     <xs:element ref="xs:annotation" minOccurs="0"/>
     <xs:choice minOccurs="1" maxOccurs="1">
      <xs:element name="all">
       <xs:complexType>
        <xs:complexContent>
         <xs:restriction base="xs:all">
          <xs:group ref="xs:allModel"/>
          <xs:attribute name="minOccurs" use="prohibited"/>
          <xs:attribute name="maxOccurs" use="prohibited"/>
         </xs:restriction>
        </xs:complexContent>
       </xs:complexType>
      </xs:element>
      <xs:element name="choice" type="xs:simpleExplicitGroup"/>
      <xs:element name="sequence" type="xs:simpleExplicitGroup"/>
     </xs:choice>
    </xs:sequence>
    <xs:attribute name="name" use="required" type="xs:NCName"/>
    <xs:attribute name="ref" use="prohibited"/>
    <xs:attribute name="minOccurs" use="prohibited"/>
    <xs:attribute name="maxOccurs" use="prohibited"/>
 </xs:complexType>

The important thing here is that the xs:choice and xs:sequence
elements have the type xs:simpleExplicitGroup, which is defined as:
 
 <xs:complexType name="simpleExplicitGroup">
  <xs:complexContent>
   <xs:restriction base="xs:explicitGroup">
    <xs:sequence>
     <xs:element ref="xs:annotation" minOccurs="0"/>
     <xs:group ref="xs:nestedParticle" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="minOccurs" use="prohibited"/>
    <xs:attribute name="maxOccurs" use="prohibited"/>
   </xs:restriction>
  </xs:complexContent>
 </xs:complexType>

As you can see, the minOccurs and maxOccurs attributes are prohibited.

So basically it's illegal to provide the minOccurs/maxOccurs
attributes on the model groups within the named groups; you need to
put these attribute on the *reference* to the group instead:

  <xs:group ref="elementsGroup" minOccurs="0" maxOccurs="unbounded" />

Hmm... or, I suppose, you might be able to get around this constraint
by putting the xs:choice within a xs:sequence, with something like:

<xs:group name="elementsGroup">
  <xs:sequence>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element ref="title" />
      <xs:element ref="creator" />
      <xs:element ref="subject" />
    </xs:choice>
  </xs:sequence>
</xs:group>

since it's an XML representation constraint. I'm not certain, though.

Cheers,

Jeni

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

Received on Monday, 4 March 2002 16:05:42 UTC