Latest web development tutorials

XML Schema complex elements - text-only

Composite elements contain only text can contain text and attributes.


Composite elements with only text

This type contains only simple content (text and attributes), therefore we would like to add this content simpleContent elements. When using simple content, we must define the extension or restrict within simpleContent element, like this:

<xs:element name="somename">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="basetype">
....
....
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

或者:

<xs:element name="somename">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="basetype">
....
....
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>

Tip: Use theextension or restriction element to expand or limit the types of simple basic elements. Here is an example of an XML element, "shoesize", which contains only text:

<shoesize country="france">35</shoesize>

The following example declares a complex type whose content is defined as an integer value, and "shoesize" element contains a property named "country" of:

<xs:element name="shoesize">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

We can also set a name for the complexType element, and let the "shoesize" element type attribute to refer to this name (by using this method, several elements can refer to the same complex type):

<xs:element name="shoesize" type="shoetype"/>

<xs:complexType name="shoetype">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>