Question

I have the following schema

<xs:element name="a">
   <xs:complexType>
     <xs:choice>
       <xs:element ref="b"/>
       <xs:element ref="c"/>
     <xs:choice>
   </xs:complexType>
<xs:element/>

How can I achieve to a to be a simple element at the same time? I want to be able for handle all cases below:

<a>TEXT</a>
<a><b/></a>
<a><c/></a>

Is it possible?

Was it helpful?

Solution

If I understand your question correctly, you want a to be a complex type with elements b or c, but also possible to be just TEXT. This can be achieved by using a choice element.

Take a look at this. With minOccurs you can specify how many times you at least want this element to occur. Put it to 0 to make the field optional.

EDIT: This is a solution, I tested it for the xml in the question and it works. You should use the mixed="true field"

<xs:element name="a">
<xs:complexType mixed="true">
    <xs:choice>
            <xs:element minOccurs="0"  ref="b"/>
            <xs:element minOccurs="0" ref="c"/>
    </xs:choice>
    <xs:attribute name="name" type="xs:string" />
</xs:complexType>
</xs:element>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top