XML Schema Complex Elements and Data Types
How to define Complex Elements and Data Types with XSD.
How do we define complex elements?
<book>
<title>Yunus Emre Divani</title>
<language>Turkish</language>
</book>
There are two ways to define complex elements.
<xs:element name="book">
<xs:complextype>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
</xs:sequence>
</xs:complextype>
</xs:element>
In the first method, we can define it inside the element.
If you think the element's structure is unique and won't be used elsewhere, you can define it like this.
If you think the created structure will be used in many elements, I recommend the second method:
<xs:element name="book" type="bookEntry"/>
<xs:element name="aboutBook" type="bookEntry"/>
<!-- You can define for one or more elements. -->
<xs:complextype name="bookEntry">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
</xs:sequence>
</xs:complextype>
You can externally include the properties of another type to the created Complex Type. From now on, I will generally use the second method in the examples.
<xs:element name="book" type="bookEntry"/>
<xs:element name="aboutBook" type="aboutBook"/>
<!-- You can define for one or more elements. -->
<xs:complextype name="bookEntry">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
</xs:sequence>
</xs:complextype>
<xs:complextype name="aboutBook">
<xs:complexcontent>
<xs:extension base="bookEntry">
<xs:sequence>
<xs:element name="author" type="xs:string"/>
<xs:element name="isbn" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexcontent>
</xs:complextype>
Empty Elements
<book isbn="9759954949"/>
To define the element in a complex way:
<xs:element name="book" type="bookEntry"/>
<xs:complextype name="bookEntry">
<xs:attribute name="isbn" type="xs:positiveInteger"/>
</xs:complextype>
You can only define isbn for the "book" element.
Element Only
<book>
<title>Yunus Emre Divani</title>
<language>Turkish</language>
</book>
If we take the XML example and define it in a complex way:
<xs:element name="book" type="bookStructure"/>
<xs:complextype name="bookStructure">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
</xs:sequence>
</xs:complextype>
Text Content Only
<book isbn="9759954949">Yunus Emre Divani</book>
To define the content of the defined book element as text only:
<xs:element name="book" type="bookStructure"/>
<xs:complextype name="bookStructure">
<xs:simplecontent>
<xs:extension base="xs:string">
<xs:attribute name="isbn" type="xs:positiveInteger" />
</xs:extension>
</xs:simplecontent>
</xs:complextype>
Mixed Content
<book>
Book Title: <title>Yunus Emre Divani</title>
Language: <language>Turkish</language>
ISBN: <isbn>9759954949</isbn>
</book>
For elements with mixed content, we define as follows:
<xs:element name="book" type="bookStructure"/>
<xs:complextype mixed="true" name="bookStructure">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complextype>
Indicators
<all> is used in any order, there must be one child element.
<xs:complextype mixed="true" name="bookStructure">
<xs:all>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:all>
</xs:complextype>
<choice> one or more child elements can be used.
<xs:complextype mixed="true" name="bookStructure">
<xs:choice>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:choice>
</xs:complextype>
<sequence> must be used in order.
<xs:complextype mixed="true" name="bookStructure">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complextype>
<maxOccurs>: determines the maximum number of times a child element can be used <minOccurs>: determines the minimum number of times a child element can be used.
<xs:complextype mixed="true" name="bookStructure">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string" maxOccurs="10" minOccurs="1"/>
</xs:sequence>
</xs:complextype>
Above, I specified that there should be minimum 1 and maximum 10 language elements.
<group> you can create a group and use it in many elements. You can also use all the indicators above inside the group.
<xs:group name="bookGroup">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
</xs:sequence>
</xs:group>
<xs:element name="book" type="bookEntry"/>
<xs:complextype name="bookEntry">
<xs:sequence>
<xs:group ref="bookGroup"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complextype>
The same applies to <attributeGroup>:
<xs:attributegroup name="bookAttribute">
<xs:attribute name="isbn" type="xs:positiveInteger"/>
</xs:attributegroup>
<xs:element name="book" type="bookEntry"/>
<xs:complextype name="bookEntry">
<xs:sequence>
<xs:attributeGroup ref="bookAttribute"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complextype>
<any> is used to extend elements. In the example below, we can define the next element as we wish.
<xs:complextype name="bookStructure">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complextype>
<anyAttribute> is used to extend attributes.
<xs:complextype name="bookStructure">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:anyAttribute/>
</xs:sequence>
</xs:complextype>
"substitutionGroup" is written for an element to substitute for another element.
<xs:element name="book" type="xs:string"/>
<xs:element name="title" substitutionGroup="book"/>
<!-- When defining XML -->
<book>Yunus Emre Divani</book>
<!-- or -->
<title>Yunus Emre Divani</title>
We can define like this. If we use block="substitution" as an attribute, we cannot make a <title></title> definition as above.
Data Types
We will examine data types.
Text Data Types
| Data Type | Description |
|---|---|
| ENTITIES | |
| ENTITY | |
| ID | An id assigned to an element as attribute. |
| IDREF | An id assigned to an element as attribute. |
| IDREFS | |
| language | Validated language id |
| Name | Validated name. |
| NCName | |
| NMTOKEN | An id assigned to an element as attribute. |
| NMTOKENS | |
| normalizedString | Text without space, tab. |
| QName | |
| string | Text |
| token | Without space, tab, etc. |
Date Data Types
| Data Type | Description |
|---|---|
| date | Date data |
| dateTime | Date and time data |
| duration | Interval data |
| gDay | Day (DD) |
| gMonth | Month (MM) |
| gMonthDay | Month - Day (MM-DD) |
| gYear | Year (YYYY) |
| gYearMonth | Year - Month (YYYY-MM) |
| time | Time |
Numeric Data Types
| Data Type | Description |
|---|---|
| byte | signed 8-bit integer |
| decimal | decimal value |
| int | signed 32-bit integer |
| integer | integer value |
| long | signed 64-bit integer |
| negativeInteger | Negative values (..,-2,-1) |
| nonNegativeInteger | Non-negative values (0,1,2,..) |
| nonPositiveInteger | Non-positive values (..,-2,-1,0) |
| positiveInteger | Positive values (1,2,..) |
| short | signed 16-bit integer |
| unsignedLong | unsigned 64-bit integer |
| unsignedInt | unsigned 32-bit integer |
| unsignedShort | unsigned 16-bit integer |
| unsignedByte | unsigned 8-bit integer |
Miscellaneous Data Types
| Data Type | Description |
|---|---|
| anyURI | |
| base64Binary | |
| boolean | |
| double | |
| float | |
| hexBinary | |
| NOTATION | |
| QName |