This section describes a tutorial example on how to specify a datatype in an Element Declaration Component.
An element declared with no specific datatype is too generic to be useful, because it can have any attributes,
sub (child) elements, and body contents. To declare an element with a specific datatype, you have 3 options:
Using an XML Scheme built-in datatype using the "type" attribute.
Using a named datatype using the"type" attribute. The named datatype is defined outside the declaration component.
Using an anonymous (inline) datatype defined inside the declaration component.
Example 1, word_builtin_datatype.xsd - Specifying the built-in datatype "string" to the root element "word".
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- word_builtin_datatype.xsd
- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<!-- Using XML Schema built-in datatype "string" -->
<xs:element name="word" type="xs:string"/>
</xs:schema>
Example 2, word_anonymous_datatype.xsd - Specifying an anonymous (inline) datatype based on the built-in "string"
to the root element "word".
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- word_anonymous_datatype.xsd
- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<!-- Using an anonymous datatype -->
<xs:element name="word">
<!-- Defining the anonymous datatype based "string" -->
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="40"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
Example 3, word_named_datatype.xsd - Specifying a named datatype, "myString" based on the built-in "string"
to the root element "word".
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- word_named_datatype.xsd
- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<!-- Using a named datatype "myString" -->
<xs:element name="word" type="myString"/>
<!-- Defining the named datatype based on "string" -->
<xs:simpleType name="myString">
<xs:restriction base="xs:string">
<xs:maxLength value="40"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
In all 3 examples above, the "word" element is declared to have a specific datatype, which dictates that
the "word" must have a string as its content with no attribute and no sub (child) elements.