This material is prepared based on the book "XML", second edition, by Kevin Howard Goldberg.
XML Schema defines two types of elements:Simple type elements use simple types . Please see Table 1 for details of simple types and their usage in defining simple type elements.
Complex type elements use complex types . Please see Table 2 for details of complex types and their usage in defining complex type elements.
Simple Types | Built-in Simple Types
XML schema includes a collection of built-in simple types for text. These include string, boolean values, URLs, various date and time format, and numbers. |
Names of built-in simple types:
<xs:element name="label" type="xs:string|xs:decimal|xs:boolean|xs:time|xs:date|xs:anyURL"/> | - means "OR". |
||
---|---|---|---|---|
Derived Custom Simple Types
By applying restrictions, or facets, on the built-in simple types, users can derive their own custom simple types. |
Anonymous Custom Type
An Anonymous Custom Type is defined inside an element definition. It can only be used for the element in which it is contained. |
Syntax for deriving an anonymous custom type:
<xs:element name="label">
<xs:simpleType>
<xs:restriction base="foundation">
<!--Specify as many restrictions (or facets) as necessary -->
<!-- For example: -->
<xs:minInclusive value="0"/>
<xs:maxExclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
||
Named Custom Type
If a custom type is used to define more than one element in an XML Schema, the custom type should be named and placed separately. |
Syntax for deriving a named custom type:
<xs:simpleType name="custom_type_name">
<xs:restriction base="foundation">
<!--Specify as many restrictions (or facets) as necessary -->
<!-- For example: -->
<xs:minInclusive value="0"/>
<xs:maxExclusive value="100"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="label" type="custom_type_name"/> |
Tips:
Complex Types
Four complex types define complex type elements. "Text only" complex type defines the structure of complex type elements with simple content. The other three complex types define the structure of complex type elements with complex content. The default derivation for complex type is: complex content that restricts anyType Based on the default condition, XML schema elements xs:complexContent and xs:anyType can be omitted from the XML Schema definitions of complex types . |
Complex Type 1 - "text only" Complex Type
This kind of complex type supports adding attribute types to simple types. |
Syntax for deriving a "text only" complex type: <xs:complexType name="complex_type_name"> <xs:simpleContent> <xs:extension base="foundation"> <!--Specify as many attributes as necessary --> <!-- For example: --> <xs:attribute name="semester" type = "xs:string"/> </xs:extension> OR <xs:restriction base="foundation"> <!--Specify as many restrictions (or facets) as necessary --> <!-- For example: --> <xs:minInclusive value="0"/> <xs:maxExclusive value="100"/> </xs:restriction> </xs:simpleContenT> </xs:complexType> The above is for named type. Defining an anonymous type is similar to the syntax in simple types. The extension element extends an existing simpleType or complexType element. |
|
---|---|---|---|
Complex Type 2 - "element only" Complex Type
Allows children and attributes. The children can be specified to appear in order , or to appear in in any order . It is also possible to specify that the complex type element contain only one child element (or a group of elements). |
Syntax for deriving an "elements only" complex type with ordered elements:
<xs:complexType name="complex_type_name"> <xs:sequence> <!- Declare the simple or complex type elements in the order in which they should appear. --> <xs:element name="child_element_name" type="child_element_type" /> . . . </xs:sequence> </xs:complexType>Tips:
<xs:complexType name="complex_type_name"> <xs:all> <!- Declare the simple or complex type elements. Order does not matter. --> <xs:element name="child_element_name" type="child_element_type" /> . . . </xs:all> </xs:complexType>Tips:
<xs:complexType name="complex_type_name"> <xs:choice> <!- Declare the simple or complex type elements. --> <xs:element name="child_element_name" type="child_element_type" /> . . . </xs:choice> </xs:complexType>Tips:
|
||
Complex Type 3 - "empty element" Complex Type
Contains only attributes. |
Syntax for deriving an "empty element" complex type:
<xs:complexType name="complex_type_name">
<!- Declare attributes -->
</xs:complexType>
An anonymous empty complex type that does not contain any attribute: <xs:complexType/> XML schema elements xs:complexContent and xs:anyType can be omitted because they are the default for the complex types. Example. |
||
Complex Type 4 - "mixed content" Complex Type
This type defines a complex element with both simple content and complex content. It allows text, child elements, and attributes. |
Syntax for deriving a "mixed content" complex type:
<xs:complexType name="complex_type_name" mixed="true"> <!- Declare a sequence, an unordered list, or a choice to specify the child elements and structure within the complex type. --> <!-- For example: <xs:choice> <xs:element name="child_element_name" type="child_element_type" /> . . . </xs:choice> --> <!- Declare the attributes that should appear in this complex type element, if any. --> </xs:complexType> See an example here. Tips:
|
© Aynur Abdurazik, 2009, all rights reserved. This document is made available for use by GMU graduate students of SWE 642. Copying, distribution or other use of this document without express permission of the author is forbidden. You may create links to pages in this web site, but may not copy all or part of the text without permission of the author.