David Norman wrote:
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.x ml">
<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.
--
Martin Honnen
http://JavaScript.FAQTs.com/