W3C home > Mailing lists > Public > xmlschema-dev@w3.org > May 2009

Re: Alternative tags in element?

From: G. Ken Holman <gkholman@CraneSoftwrights.com>
Date: Sat, 02 May 2009 22:20:29 -0400
Message-Id: <7.0.1.0.2.20090502221652.025b23f8@wheresmymailserver.com>
To: "xmlschema-dev@w3.org" <xmlschema-dev@w3.org>
At 2009-04-30 22:06 +1000, Paul Steckler wrote:
>   I want to create an element, say "Foo" that contains a sequence 
> of elements which
>have different tags, say "Bar" and "Glob".  So these would be legal:
>
>    <Foo><Glob/><Bar/></Foo>
>
>   <Foo><Bar/><Glob/></Foo>
>
>   <Foo><Bar/><Glob/><Bar/></Foo>
>
>In the complexType for Foo elements, can you specify that it 
>contains a sequence
>of alternative elements, in arbitrary order?

Yes, it could be called an "unbounded choice".

Unless, perhaps, by "alternative" you mean that they alternate from 
one to the other?  I'm guessing not.

>Or do I have to create a wrapper element
>for the alternatives and place them in a union?

Nope.

I hope the example below helps.

. . . . . . . Ken

t:\ftemp>type steckler1.xml
<Foo><Glob/><Bar/></Foo>

t:\ftemp>type steckler2.xml
<Foo><Bar/><Glob/></Foo>

t:\ftemp>type steckler3.xml
<Foo><Bar/><Glob/><Bar/></Foo>

t:\ftemp>type steckler4-bad.xml
<Foo><Bar/><Baz/></Foo>

t:\ftemp>type steckler.xsd
<?xml version="1.0" encoding="US-ASCII"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:annotation>
  <xsd:documentation xml:lang="en">
    Paul's example
  </xsd:documentation>
</xsd:annotation>

<xsd:element name="Foo">
   <xsd:complexType>
     <xsd:choice maxOccurs="unbounded">
       <xsd:element name="Bar"/>
       <xsd:element name="Glob"/>
     </xsd:choice>
   </xsd:complexType>
</xsd:element>

</xsd:schema>

t:\ftemp>w3cschema-saxon steckler.xsd steckler1.xml
No validation errors.

t:\ftemp>w3cschema-saxon steckler.xsd steckler2.xml
No validation errors.

t:\ftemp>w3cschema-saxon steckler.xsd steckler3.xml
No validation errors.

t:\ftemp>w3cschema-saxon steckler.xsd steckler4-bad.xml
Validation error on line 1 column 18 of steckler4-bad.xml:
   In content of element <Foo>: The content model does not allow 
element <Baz> to appear
   here. Expected one of: Bar, Glob (See 
http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4)
No validation errors.

t:\ftemp>

--
XQuery/XSLT/XSL-FO hands-on training - Los Angeles, USA 2009-06-08
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/x/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@CraneSoftwrights.com
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/x/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal
Received on Sunday, 3 May 2009 02:22:37 GMT

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