Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Schema: Element that can contain elements or text?

Tags:

xml

xsd

How would I define an element that can either contain plain text or contain elements? Say I wanted to somehow allow for both of these cases:

<xs:element name="field">     <xs:complexType>         <xs:sequence>             <xs:element ref="subfield" minOccurs="0" maxOccurs="unbounded" />         </xs:sequence>         <xs:attribute name="name" type="xs:string" />     </xs:complexType> </xs:element>  <xs:element name="field" type="xs:string" /> 

... so that both these elements would be valid:

<field name="test_field_0">     <subfield>Some text.</subfield> </field>  <field name="test_field_1">Some more text.</field> 
like image 457
Wilco Avatar asked Dec 19 '08 18:12

Wilco


People also ask

What XML element that contains other elements and or attributes?

A complex element is an XML element that contains other elements and/or attributes.

Which element is used only in the context of the text?

Explanation: Simple type element is used only in the context of the text.

Which schema type can contain sub elements and attributes?

An XML schema describes the coarse shape of the XML document, what fields an element can contain, which sub elements it can contain etc, it can also describe the values that can be placed into any element or attribute.

What is simple element and complex element in XML?

A simple element is an XML element that does not have any attributes or any sub (child) elements. A simple element can be declared with a simple datatype. What is a complex element? A complex element is an XML element that have at least one attribute, or at least one sub (child) element.


1 Answers

I did some research on this a while ago and the only solution I found was to used the mixed attribute:

<xs:element name="field">     <xs:complexType mixed="true">         <xs:sequence>                 <xs:element ref="subfield" minOccurs="0" maxOccurs="unbounded" />         </xs:sequence>         <xs:attribute name="name" type="xs:string" />     </xs:complexType> </xs:element> 

This sadly also allows

<field name="test_field_0">     Some text I'm sure you don't want.     <subfield>Some text.</subfield>     More text you don't want. </field> 

Hopefully someone will give a better answer.

like image 183
David Norman Avatar answered Sep 28 '22 02:09

David Norman