|
XSD Syntax
Part:
1
2
3
(Continued from previous part...)
Simple XML Elements with Extended Data Types
Simple XML Element: An XML element that has no child elements and attributes.
Simple XML elements can be defined by using the pre-defined XSD data types. They
can also be defined by using extended data types, which are defined by "simpleType"
statements:
<xsd:simpleType name="my_type_name">
<xsd:restriction base="xsd:type_name">
XSD facet statements
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="element_name" type="my_type_name"/>
where "element_name" is the name of the XML element, "xsd:type_name" is
a pre-defined data type serving as the base data type, and "my_type_name"
is the new data type extended from the base data type.
Optional facet statements can be enclosed in the "restriction" statement to provide
additional restrictions to the extended data type:
- xsd:minInclusive - Inclusive minimum value.
- xsd:minExclusive - Exclusive minumum value.
- xsd:maxInclusive - Inclusive maximum value.
- xsd:maxExclusive - Exclusive maximum value.
- xsd:enumeration - One valid value. Repeat this statement to provide multiple values.
- xsd:whiteSpace - How to handle white spaces: "preserve|replace|collapse".
- xsd:pattern - A regular expression the value must match.
- xsd:length - Number of characters.
- xsd:minLength - Minimum number of characters.
- xsd:maxLength - Maximum number of characters.
- xsd:totalDigits - Maximum number of digits.
- xsd:fractionDigits - Maximum number of digits in the fraction part.
The syntax of facet statement is:
<xsd:facet_name value="facet_value">
Besides restricting the base data types to extend new data types, we can also extend
new data types by define "union" or "list" of base data types.
Union: A collection of base data types. A value is valid for a union data type
if and only it is valid for one of the base data types.
<xsd:simpleType name="my_type_name">
<union>
<xsd:restriction base="xsd:type_name_1">
XSD facet statements
</xsd:restriction>
<xsd:restriction base="xsd:type_name_2">
XSD facet statements
</xsd:restriction>
</union>
</xsd:simpleType>
<xsd:element name="element_name" type="my_type_name"/>
List: A data type for lists of values of a base data type. The list of values must
be white space delimited. For example, the following code
<xsd:simpleType name="integerList">
<list>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1">
<xsd:maxInclusive value="49">
</xsd:restriction>
</list>
</xsd:simpleType>
<xsd:element name="winning_numbers" type="integerList"/>
defines that "winning_numbers" element is allowed to have a list of number in the range of
1 and 49.
Complex XML Elements
Complex XML Element: An XML element that has at least one child element or
at least one attribute. Complex XML elements must be defined with complex data types,
which are defined by "complexType" statements:
<xsd:element name="element_name" type="my_type_name"/>
<xsd:complexType name="my_data_type">
<xsd:sequence>
<xsd:element name="child_element_1" type="data_type_1"/>
<xsd:element name="child_element_2" type="data_type_2"/>
...
</xsd:sequence>
<xsd:attribute name="attribute_a" type="data_type_a"/>
<xsd:attribute name="attribute_b" type="data_type_b"/>
...
</xsd:complexType>
where "attribute" statement is used to define an attribute, and "sequence" statement
is used to define the group of child elements, and the order the child elements should
appear in the XML structure.
Note that "attribute" statements must appear after the child element definition
statements.
Additional options are available for the "element" statement enclosed in the
"sequence" statement:
<xsd:sequence>
<xsd:element name="child_element" type="data_type"
minOccurs="0|1|...|unbounded" maxOccurs="0|1|...|unbounded"
mixed="true|false"/>
</xsd:sequence>
where:
- "minOccurs" specifies the minimum number of occurrences. Default is "1".
- "maxOccurs" specifies the maximum number of occurrences. Default is "1".
- "mixed" specifies if child text nodes are allowed. Default is "false".
There is one additional options available for the "attribute" statement:
<xsd:attribute name="attribute" type="data_type"
use="optional|required"/>
where:
- "use" specifies if this attribute is optional. Default is "optional".
"sequence" is not the only way to group child elements. XSD provides 3 ways of grouping
child element:
- xsd:sequence - Enclosed elements should appear in the specified order.
- xsd:all - Enclosed elements can appear in any order.
- xsd:choice - Only one of the enclosed elements should appear.
Of course, grouping statements can be nested.
Simple Content XML Elements
Simple Content XML Element: A special complex XML element that has
at least one attribute and child text nodes. Simple content XML elements must
be defined with complex data types in the following format:
<xsd:complexType name="my_data_type">
<xsd:simpleContent>
<xsd:extension base="xsd:data_type">
<xsd:attribute name="attribute_a" type="data_type_a"/>
<xsd:attribute name="attribute_b" type="data_type_b"/>
...
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
(Continued on next part...)
Part:
1
2
3
|