Re: Recursion in an XML Schema

Jeni Tennison <jeni@jenitennison.com> writes:

> Hi Matthew,
> 
> > I figure the easiest way to do this is to use recursion in the XML
> > schema to say something like:
> >
> > <schema>
> >     <complexType name="SectionType">
> >         <sequence>
> >             <element name="section" type="SectionType">
> >               <annotation>
> >                   <documentation>Recursive</documentation>
> >               </annotation>
> >             </element>
> >         </sequence>
> >     </complexType>
> >     <element name="section" type="SectionType"/>
> > </schema>
> >
> > is this a valid method of using recursion inside a schema, and if
> > not, how can I go about doing it?
> 
> That's a valid way to do it. The one thing you have to be a little
> careful of is the fact that the section element declared at the top
> level of the document is subtly different from the section element
> declared inside the complex type. In particular, the section element
> declared within the complex type might not be qualified (depending on
> what you've done with the elementFormDefault), but this is only a
> worry if you dealing with namespaces.
> 
> Of course there are several alternative designs that do the same
> thing. You could reference the global element declaration from within
> the complex type definition:
> 
> <complexType name="SectionType">
>   <sequence>
>     <element ref="section" />
>   </sequence>
> </complexType>
> 
> And you could make the complex type definition anonymous:
> 
> <element name="section">
>   <complexType>
>     <sequence>
>       <element ref="section" />
>     </sequence>
>   </complexType>
> </element>

Note that as written both the original and these modifications of it,
although valid schemas, require infinite documents with no actual
textual content, just nested <section>s all the way down!

You probably want something more like

<element name="section">
  <complexType mixed="true">
              ^^^^^^^^^^^^^
    <sequence minOccurs="0" maxOccurs="unbounded">
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      <element ref="section" />
    </sequence>
  </complexType>
</element>

ht
-- 
  Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
          W3C Fellow 1999--2001, part-time member of W3C Team
     2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
	    Fax: (44) 131 650-4587, e-mail: ht@cogsci.ed.ac.uk
		     URL: http://www.ltg.ed.ac.uk/~ht/

Received on Thursday, 1 November 2001 09:12:05 UTC