XML Schema Complex Elements and Data Types
How to define Complex Elements and Data Types with XSD?
How do we define complex elements?
<kitap>
<adi>Yunus Emre Divanı</adi>
<dil>Türkçe</dil>
</kitap>
There are two ways to define complex elements.
<xs:element name="kitap">
<xs:complexType>
<xs:sequence>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
In the first method, we can define the element inside itself.
If you think the structure of the element is specific to itself and won't be used elsewhere, you can define it this way.
If you believe that the structure created will be used in many elements, I recommend the second method:
<xs:element name="kitap" type="kitapGiris"/>
<xs:element name="kitapHakkinda" type="kitapGiris"/>
<!-- You can define for one or multiple elements. -->
<xs:complexType name="kitapGiris">
<xs:sequence>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
</xs:sequence>
</xs:complexType>
You can include the properties of another type externally. In the following examples, I will generally use the second method.
<xs:element name="kitap" type="kitapGiris"/>
<xs:element name="kitapHakkinda" type="kitapHakkinda"/>
<!-- You can define for one or multiple elements. -->
<xs:complexType name="kitapGiris">
<xs:sequence>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="kitapHakkinda">
<xs:complexContent>
<xs:extension base="kitapGiris">
<xs:sequence>
<xs:element name="yazar" type="xs:string"/>
<xs:element name="isbn" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Empty Elements
<kitap isbn="9759954949"/>
To define the element in a complex way:
<xs:element name="kitap" type="kitapGiris"/>
<xs:complexType name="kitapGiris">
<xs:attribute name="isbn" type="xs:positiveInteger"/>
</xs:complexType>
You can only define the isbn attribute for the "kitap" element.
Element Only
<kitap>
<adi>Yunus Emre Divanı</adi>
<dil>Türkçe</dil>
</kitap>
If we take an XML example and define it in a complex way:
<xs:element name="kitap" type="kitapYapi"/>
<xs:complexType name="kitapYapi">
<xs:sequence>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Text-Only Content
<kitap isbn="9759954949">Yunus Emre Divanı</kitap>
To define the content of the "kitap" element as text only:
<xs:element name="kitap" type="kitapYapi"/>
<xs:complexType name="kitapYapi">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="isbn" type="xs:positiveInteger" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
Mixed Content
<kitap>
Kitap Adı : <adi>Yunus Emre Divanı</adi>
Dil : <dil>Türkçe</dil>
ISBN : <isbn>9759954949</isbn>
</kitap>
To define mixed content, we use the following:
<xs:element name="kitap" type="kitapYapi"/>
<xs:complexType mixed="true" name="kitapYapi">
<xs:sequence>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
Indicators
<all>
is used inside which all child elements can be used, and each child element must appear only once.
<xs:complexType mixed="true" name="kitapYapi">
<xs:all>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:all>
</xs:complexType>
<choice>
allows one or more child elements to be used.
<xs:complexType mixed="true" name="kitapYapi">
<xs:choice>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:choice>
</xs:complexType>
<sequence>
enforces the order of appearance.
<xs:complexType mixed="true" name="kitapYapi">
<xs:sequence>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
<maxOccurs>
specifies the maximum number of occurrences of the child element. <minOccurs>
specifies the minimum number of occurrences of the child element.
<xs:complexType mixed="true" name="kitapYapi">
<xs:sequence>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string" maxOccurs="10" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
In the above example, it is specified that the "dil" element must appear at least once (minOccurs="1") and can appear up to 10 times (maxOccurs="10").
The same applies to <group>
. You can use all the indicators mentioned above inside a group.
<xs:group name
="kitapGrup">
<xs:sequence>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
</xs:sequence>
</xs:group>
<xs:element name="kitap" type="kitapGiris"/>
<xs:complexType name="kitapGiris">
<xs:sequence>
<xs:group ref="kitapGrup"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
The same applies to <attributeGroup>
as well.
<xs:attributeGroup name="kitapEtiket">
<xs:attribute name="isbn" type="xs:positiveInteger"/>
</xs:attributeGroup>
<xs:element name="kitap" type="kitapGiris"/>
<xs:complexType name="kitapGiris">
<xs:sequence>
<xs:attributeGroup ref="kitapEtiket"/>
<xs:element name="isbn" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
<any>
is used to expand the element. In the following example, we can define the next element as we wish.
<xs:complexType name="kitapYapi">
<xs:sequence>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<anyAttribute>
is used to expand attributes.
<xs:complexType name="kitapYapi">
<xs:sequence>
<xs:element name="adi" type="xs:string"/>
<xs:element name="dil" type="xs:string"/>
<xs:anyAttribute/>
</xs:sequence>
</xs:complexType>
"\substitutionGroup" is used to indicate that an element can be substituted for another element.
<xs:element name="kitap" type="xs:string"/>
<xs:element name="adi" substitutionGroup="kitap"/>
<!-- When defining XML -->
<kitap>Yunus Emre Divanı</kitap>
<!-- or -->
<adi>Yunus Emre Divanı</adi>
In this way, we can define the <adi></adi>
element as a substitution for the <kitap></kitap>
element. If you use block="substitution", then you won't be able to do the above like <adi></adi>
.
Data Types
Let's examine the data types.
Text Data Types
Data Type | Description |
---|---|
ENTITIES | |
ENTITY | |
ID | An id assigned as an attribute to an element. |
IDREF | An id assigned as an attribute to an element. |
IDREFS | |
language | A validated language id. |
Name | A validated name. |
NCName | |
NMTOKEN | An id assigned as an attribute to an element. |
NMTOKENS | |
normalizedString | Text with no spaces or tabs. |
QName | |
string | Text |
token | Text with no spaces or tabs. |
Date Data Types
Data Type | Description |
---|---|
date | Date value |
dateTime | Date and time value |
duration | Interval in time |
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 |
Mixed Data Types
Data Type | Description |
---|---|
anyURI | |
base64Binary | |
boolean | |
double | |
float | |
hexBinary | |
NOTATION | |
QName |