Declaring a Global Complex Type
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" ...> <xsd:complexType name="type-name"> ... </xsd:complexType> </schema>
A complexType declared as a direct child of the schema root element is considered a 'global' complexType.
A 'name' attribute is required for a global defined complexType.
Referencing a Global Complex Type
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" ...> <xsd:element name="element-name" type="type-name"/> <xsd:complexType name="type-name"> ... </xsd:complexType> </schema>
Reuse a global defined complexType, by referencing the complexType, using the 'type' attribute.
A complex type can only be specified for elements.
Declaring a Local Complex Type
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" ...> <xsd:element name="element-name"> <xsd:complexType> ... </xsd:complexType> <attribute> </schema>
A complexType declared as a child of an element is considered a 'local' complexType.
The type attribute cannot be used together with a local complexType element.
A local complex type does not have a name.
SimpleContent
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" ...> <xsd:complexType name="type-name"> <xsd:simpleContent> ... </xsd:simpleContent> </xsd:complexType> </schema>
Specifies text content and/or attributes.
ComplexContent
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" ...> <xsd:complexType name="type-name"> <xsd:complexContent> ... </xsd:complexContent> </xsd:complexType> </schema>
Specifies element or mixed content and/or attributes.
Derivation by restriction.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" ...> <xsd:complexType name="type-name"> <xsd:complexContent> <xsd:restriction base="complex-type-name"> ... </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="another-type-name"> <xsd:simpleContent> <xsd:restriction base="xs:string"> ... </xsd:restriction> </xsd:simpleContent> </xsd:complexType> </schema>
Specify the to be restricted type using the 'base' attribute or with an anonymous inner class.
Remove elements/attributes from the base-type.
It is mandatory to repeat the content from the base-type.
Result always validates against base-type.
Derivation by extension.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" ...> <xsd:complexType name="type-name"> <xsd:complexContent> <xsd:extension base="complex-type-name"> ... </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="another-type-name"> <xsd:simpleContent> <xsd:extension base="xs:string"> ... </xsd:extension> </xsd:simpleContent> </xsd:complexType> </schema>
Specify the to be extended type using the 'base' attribute or with an anonymous inner class.
Add elements/attributes to the base-type.
Result does not always validate against base-type.
Defining attributes in complexTypes.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" ...> <xsd:complexType name="type-name"> <xsd:complexContent> <xsd:extension base="complex-type-name"> ... <xsd:attribute name="attribute-name" type="xs:string"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="another-type-name"> <xsd:simpleContent> <xsd:restriction base="xs:string"> ... <xsd:attribute name="attribute-name" type="xs:string"/> </xsd:restriction> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name="another-type-name"> ... <xsd:attribute name="attribute-name" type="xs:string"/> </xsd:complexType> </schema>
Attributes can be defined as child elements of the restriction/extension elements.
Attributes can also be defined directly on the complexType.
Attributes are always defined after all facets or content models have been defined.
Defining empty elements.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" ...> <xsd:element name="empty-element"> <xsd:complexType> <xsd:complexContent> <xsd:restriction base="xsd:anyType"/> </xsd:complexContent> </xsd:complexType> </xsd:element> </schema>
Defines an element with a complex-type with complex-content that has no content defined.