XML: How to require either or both of two child elements, but prevent both missing?

William at William at
Sat Feb 22 17:08:51 CET 2003


Hello:

Wouldn´t this allow for the case when there are two A's or two B's?

I haven't tried it yet but, I just glanced at the spec and found an example that would yield something like this if applied:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Parent">
        <xs:complexType>
           <xs:choice>
                   <xs:group   ref="elementABopt"/>
                   <xs:element ref="elementB" minOccurs="1"/>
        </xs:complexType>
</xs:element>

<xs:element name="elementAoptB">
        <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="elementA" minOccurs="1"/>
                        <xs:element ref="elementB" minOccurs="0"/>
                    </xs:sequence>
        </xs:complexType>
</xs:element>

<xs:element name="elementA">
</xs:element>

<xs:element name="elementB">
</xs:element>

</xs:schema>

Do you see any problem here (besides complexity?)

Hope this helps...

Greetings,

William.



-----Original Message-----
From: Jim Croft [mailto:jrc at ANBG.GOV.AU]
Sent: Sábado 22 de Febrero de 2003 04:43 PM
To: TDWG-SDD at USOBI.ORG
Subject: Re: XML: How to require either or both of two child elements,
but prevent both missing?


>How in XML-schema do you make sure that of two possible child
>elements, either or both, but not neither are present?

How about changing the model to 'choice' and requiring one but allowing two
(or multiple) occurrences of optional elements?

something like this maybe?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
         <xs:element name="Parent">
                 <xs:complexType>
                         <xs:choice minOccurs="1" maxOccurs="2">
                                 <xs:element name="A" minOccurs="0"
maxOccurs="1"/>
                                 <xs:element name="B" minOccurs="0"
maxOccurs="1"/>
                         </xs:choice>
                 </xs:complexType>
         </xs:element>
</xs:schema>




~ Jim Croft ~ jrc at anbg.gov.au ~ 02-62465500 ~ www.anbg.gov.au/jrc/ ~




More information about the tdwg-content mailing list