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 TypeDescription
ENTITIES
ENTITY
IDAn id assigned to an element as attribute.
IDREFAn id assigned to an element as attribute.
IDREFS
languageValidated language id
NameValidated name.
NCName
NMTOKENAn id assigned to an element as attribute.
NMTOKENS
normalizedStringText without space, tab.
QName
stringText
tokenWithout space, tab, etc.

Date Data Types

Data TypeDescription
dateDate data
dateTimeDate and time data
durationInterval data
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

Miscellaneous Data Types

Data TypeDescription
anyURI
base64Binary
boolean
double
float
hexBinary
NOTATION
QName