Option between simple OR complex types in an XML Schema?

S

Steve

Let's say I have an element that can contain a simple child element, OR
a child element of complex type (but not both). For example, an actual
XML snippet might look like this...

<ValueMapping>
<LocalValue>abc123</LocalValue>
<RemoteValue>123abc</RemoteValue>
</ValueMapping>

....or the XML might look like this...

<ValueMapping>
<LocalValue>abc123</LocalValue>
<MultiValueList>
<RemoteValue>123</RemoteValue>
<RemoteValue>abc</RemoteValue>
</MultiValueList>
</ValueMapping>


Is there an XML Schema I could write that would support both of these
possibilies... allowing the "RemoteValue" to be either a simple element
or a complex type of multiple elements (but not both at the same time)?
Thanks in advance!
 
M

Martin Honnen

Steve said:
Let's say I have an element that can contain a simple child element, OR
a child element of complex type (but not both). For example, an actual
XML snippet might look like this...

<ValueMapping>
<LocalValue>abc123</LocalValue>
<RemoteValue>123abc</RemoteValue>
</ValueMapping>

...or the XML might look like this...

<ValueMapping>
<LocalValue>abc123</LocalValue>
<MultiValueList>
<RemoteValue>123</RemoteValue>
<RemoteValue>abc</RemoteValue>
</MultiValueList>
</ValueMapping>


Is there an XML Schema I could write that would support both of these
possibilies... allowing the "RemoteValue" to be either a simple element
or a complex type of multiple elements (but not both at the same time)?

You can use xs:choice likt this:

<xs:element name="ValueMapping">
<xs:complexType>
<xs:sequence>
<xs:element name="LocalValue" type="xs:string"/>
<xs:choice>
<xs:element name="RemoteValue" type="xs:string"/>
<xs:element name="MultiValueList">
<xs:complexType>
<xs:sequence>
<xs:element name="RemoteValue" type="xs:string"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top