Re: mixing childs in any order and any quantity

Hi Fermín,

This should be okay:


----------
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!--
   Schema where title/chapter/author are all 
optionally, indicated by brackets [] and where 
chapter can occur multiple times, indicates by asterisk *.
-->

  <xs:element name="book">
    <xs:complexType>
      <xs:sequence>
        <xs:group ref="chapters"/>
        <xs:choice>
          <xs:group ref="chapters"/> <!-- no title/author -->
          <xs:group ref="titlefirst"/>
          <xs:group ref="authorfirst"/>
        </xs:choice>
        <xs:group ref="chapters"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <!-- [chapter]* -->
  <xs:group name="chapters">
    <xs:sequence>
      <xs:element ref="chapter" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:group>

  <!-- title [chapter]* [author] -->
  <xs:group name="titlefirst">
    <xs:sequence>
      <xs:element ref="title"/>
      <xs:group ref="chapters"/>
      <xs:element ref="author" minOccurs="0"/>
    </xs:sequence>
  </xs:group>

  <!-- author [chapter]* [title] -->
  <xs:group name="authorfirst">
    <xs:sequence>
      <xs:element ref="author"/>
      <xs:group ref="chapters"/>
      <xs:element ref="title" minOccurs="0"/>
    </xs:sequence>
  </xs:group>

  <xs:element name="title">
    <xs:complexType mixed="true"/>
  </xs:element>

  <xs:element name="author">
    <xs:complexType mixed="true"/>
  </xs:element>

  <xs:element name="chapter">
    <xs:complexType mixed="true"/>
  </xs:element>

</xs:schema>

----------

I checked it at http://www.xmlme.com/Validator.aspx with

<?xml version="1.0"?>
<book>
    <chapter>...</chapter>
    <author>...</author>
    <chapter>...</chapter>
    <chapter>...</chapter>
    <chapter>...</chapter>
    <title>...</title>
    <chapter>...</chapter>
</book>

And your examples are okay too.

This is of course a workaround, but it works. :-)

Kind regards,

Anne

At 11:54 6-10-2006, Fermín Galán Márquez wrote:

>Hi,
>
>I'm trying to define using XML Schema a element which content mixes several
>elements, some of then multiple, that can come in any order and I wonder if
>even this is possible.
>
>Let me clarify with a example. Consider a <book> element whose content can
>be up to one <title> (minOccurs=0, maxOccurs=1), up to one <author>
>(minOccurs=0, maxOccurs=1) and many <chapter> (minOccurs=0,
>maxOccurs=unbounded) that comes together, but combining child tags in any
>order.
>
>Examples:
>
><book>
>    <title>...</title>
>    <chapter>...</chapter>
>    <chapter>...</chapter>
>    <chapter>...</chapter>
>    <author>...</autor>
></book>
>
><book>
>    <title>...</title>
>    <author>...</autor>
>    <chapter>...</chapter>
>    <chapter>...</chapter>
>    <chapter>...</chapter>
></book>
>
>How would be the XML Schema definition for such <book> element, please?
>Firstly, I though using <xs:all>, but the problem is that it only allows
>elements with maxOccurs=1, so it doesn't fit with <chapter> (that has
>maxOccurs=unbounded).
>
>Anybody knows a solution or workaround, please? I'm getting crazy reviewing
>the XML Schema reference for some solution, but I'm not finding anything ..
>Please, any help is very welcome!
>
>Regards,
>
>--------------------
>Fermín Galán Márquez
>CTTC - Centre Tecnològic de Telecomunicacions de Catalunya
>Parc Mediterrani de la Tecnologia, Av. del Canal Olímpic s/n, 08860
>Castelldefels, Spain
>Room 1.02
>Tel : +34 93 645 29 12
>Fax : +34 93 645 29 01
>Email address: fermin.galan@cttc.es
>
>PD. Of course I could do something like:
>
><book>
>    <title>...</title>
>    <author>...</autor>
>    <chapters>
>       <chapter>...</chapter>
>       <chapter>...</chapter>
>       <chapter>...</chapter>
>    </chapters>
></book>
>
>and define <book> as a <xs:all> of <title>, <author> and <chapters>, but I
>would like to solve the problem without adding the <chapters> "grouping"
>element.

Received on Friday, 6 October 2006 17:45:57 UTC