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

Re: xsd:group and minOccurs

From: Jeni Tennison <jeni@jenitennison.com>
Date: Fri, 30 Aug 2002 01:39:01 +0100
Message-ID: <1551508946660.20020830013901@jenitennison.com>
To: xmlschema-dev@w3.org, Eddie Robertsson <erobertsson@allette.com.au>
CC: "Gregory M. Messner" <gmessner@breezefactor.com>

Hi Eddie,

>>A model group like:
>>
>>  <xsd:choice>
>>    <xsd:element name="A" minOccurs="0" type="xsd:string" />
>>    <xsd:element name="B" minOccurs="0" type="xsd:string" />
>>    <xsd:element name="C" minOccurs="0" type="xsd:string" />
>>  </xsd:choice>
>>
>>is a choice model group that has three element particles. All those
>>particles are *optional* (have a minimum occurrence of 0) but the
>>model group still has particles. So this is perfectly valid
>>(thankfully!).
>
> Hmm, yes the above is a perfectly legal schema but was my conclusion
> correct that an instance document which is empty for the above
> content model would be invalid? Example:
>
> <xsd:element name="Test">
>   <xsd:complexType>
>     <xsd:choice>
>       <xsd:element name="A" minOccurs="0" type="xsd:string" />
>       <xsd:element name="B" minOccurs="0" type="xsd:string" />
>       <xsd:element name="C" minOccurs="0" type="xsd:string" />
>     </xsd:choice>
>   </xsd:complexType>
> </xsd:element>
>
> Would an instance document that only has:
>
> <Test/>
>
> be valid or invalid?

Valid.

> If this is valid then can you give an example where the above rule
> that "having a choice between an empty set of particles is invalid"
> is broken?

Sure:

<xsd:element name="Test">
  <xsd:complexType>
    <xsd:choice />
  </xsd:complexType>
</xsd:element>

This is an element declaration for the Test element, with an anonymous
complex type whose content model is the single particle: a 'choice'
model group that has no particles.

When the instance:

  <Test />

is assessed against this schema, it's invalid because clause 2 of
Validation Rule: Element Sequence Valid [1] is not met. The clause
reads:

  If the {compositor} is choice, then there must be a particle among
  the {particles} such that the sequence is ·valid· with respect to
  that particle as defined in Element Sequence Locally Valid
  (Particle) (§3.9.4).

Here, the model group's {compositor} is choice, but the model group
does not have any {particles}, so there is no particle amongst the
particles that is valid with respect to the content of the Test
element.

In fact, it's impossible to create an instance that is valid against
that schema, which is why the note under the validation rule says:

  no sequence can be ·valid· with respect to [a group whose
  {particles} is empty] whose {compositor} is choice

For some reason this isn't a constraint that's checked at the schema
level -- you only work out that your schema isn't ever going to work
when you try to validate instances against it.
  
BTW, note that "particles" are the things that live inside model
groups (they're things in the schema). I have a feeling that you're
interpreting "particles" to mean "elements in content in an instance",
which I don't think is right. For example, even if the instance were:

  <Test>
    <foo />
  </Test>

the choice model group would still have no particles, and if you have:

<xsd:element name="Test">
  <xsd:complexType>
    <xsd:choice>
      <xsd:element name="A" minOccurs="0" type="xsd:string" />
      <xsd:element name="B" minOccurs="0" type="xsd:string" />
      <xsd:element name="C" minOccurs="0" type="xsd:string" />
    </xsd:choice>
  </xsd:complexType>
</xsd:element>

the choice has three (element declaration) particles, regardless of
whether the Test element in the instance document has any content or
not.
  
Cheers,

Jeni

[1] http://www.w3.org/TR/xmlschema-1/#cvc-model-group

---
Jeni Tennison
http://www.jenitennison.com/
Received on Thursday, 29 August 2002 20:39:03 GMT

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