XML Schema Q: Element with either simpleContent or complexContent

D

David Norman

How can I create an xml schema where both of the following documents
are valid?

<theElement>
<child>some text</child>
</theElement>

and

<theElement>1234</theElement>

Basically, <theElement> should either contain a single element <child>
of a specified type or an integer.

Thanks,
David
 
M

Martin Honnen

David said:
How can I create an xml schema where both of the following documents
are valid?

<theElement>
<child>some text</child>
</theElement>

and

<theElement>1234</theElement>

Basically, <theElement> should either contain a single element <child>
of a specified type or an integer.


If you use

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="theElement" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="theElement">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="child" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

that is you specify the element to have a complex type where mixed
content is allowed then the following validates:

<?xml version="1.0" encoding="UTF-8"?>
<root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2004073101Xsd.xml">
<theElement>
<child>some text</child>
</theElement>
<theElement>1234</theElement>
</root>

However the schema also allows a <theElement> as follows
<theElement>1234<child>Kibology</child>1234</theElement>

If you want to allow either the simple content of type integer or a
complex content of exactly one child element you would need to have a
union of both types however I think a union is only possible for simple
types.
 
D

David Norman

Martin Honnen said:
If you use

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="theElement" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="theElement">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="child" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

that is you specify the element to have a complex type where mixed
content is allowed then the following validates:

<?xml version="1.0" encoding="UTF-8"?>
<root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2004073101Xsd.xml">
<theElement>
<child>some text</child>
</theElement>
<theElement>1234</theElement>
</root>

However the schema also allows a <theElement> as follows
<theElement>1234<child>Kibology</child>1234</theElement>

If you want to allow either the simple content of type integer or a
complex content of exactly one child element you would need to have a
union of both types however I think a union is only possible for simple
types.

Because of the last possibility, mixed="true" doesn't give me what I want.

Thanks,
David
 
P

Peter Flynn

David said:
How can I create an xml schema where both of the following documents
are valid?

<theElement>
<child>some text</child>
</theElement>

and

<theElement>1234</theElement>

Basically, <theElement> should either contain a single element <child>
of a specified type or an integer.

This sounds to me like extremely poor data design.

///Peter
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top