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 TypeDescription
ENTITIES
ENTITY
IDAn id assigned as an attribute to an element.
IDREFAn id assigned as an attribute to an element.
IDREFS
languageA validated language id.
NameA validated name.
NCName
NMTOKENAn id assigned as an attribute to an element.
NMTOKENS
normalizedStringText with no spaces or tabs.
QName
stringText
tokenText with no spaces or tabs.

Date Data Types

Data TypeDescription
dateDate value
dateTimeDate and time value
durationInterval in time
gDayDay (DD)
gMonthMonth (MM)
gMonthDayMonth - Day (MM-DD)
gYearYear (YYYY)
gYearMonthYear - Month (YYYY-MM)
timeTime

Numeric Data Types

Data TypeDescription
byteSigned 8-bit integer
decimalDecimal value
intSigned 32-bit integer
integerInteger value
longSigned 64-bit integer
negativeIntegerNegative values (..,-2,-1)
nonNegativeIntegerNon-negative values (0,1,2,..)
nonPositiveIntegerNon-positive values (..,-2,-1,0)
positiveIntegerPositive values (1,2,..)
shortSigned 16-bit integer
unsignedLongUnsigned 64-bit integer
unsignedIntUnsigned 32-bit integer
unsignedShortUnsigned 16-bit integer
unsignedByteUnsigned 8-bit integer

Mixed Data Types

Data TypeDescription
anyURI
base64Binary
boolean
double
float
hexBinary
NOTATION
QName