XML Schema is a powerful alternative to XML DTD. The XML Schema language is referred to as XML Schema Definition (XSD).

Why XML Schema:

  • Supports data types.
  • Written with XML syntax.
  • Provides secure data communication.
  • Flexible.

The <schema> element

The root element is the <schema> element.

<xml version="1.0"?>
<xs:schema>
<!-- Code Block -->
</xs:schema>

The <schema> element's attributes:

<xml version="1.0"?>
<xs:schema
		xmlns:xs="http://www.w3.org/2001/XMLSchema"
		targetNamespace="https://example.com"
		xmlns="https://example.com">
<!-- Code Block -->
</xs:schema>

xmlns:xs attribute indicates that we will make definitions according to http://www.w3.org/2001/XMLSchema standards.

targetNamespace attribute indicates that the attributes defined in XML come from this namespace.

xmlns attribute indicates the namespace this XML belongs to.

If we want to call XSD as a reference:

<xml version="1.0"?>
<books xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://example.com/books.xsd">
<!-- XML -->

xsi:schemaLocation attribute specifies which address's XSD will be applied to the element it's in.

Simple Elements

Element Definition

To define elements in XML with XSD:

<xs:element name="element-name" type="element-type"/>

Element Types:

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

Let's apply element types to our library example. Example XML:

<xml version="1.0"?>
<title>Yunus Emre Divani</title>
<author>Selim Yagmur</author>
<language>Turkish</language>
<edition>8</edition>
<date>2014/04/01</date>

Let's define with XSD:

<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element name="edition" type="xs:integer"/>
<xs:element name="date" type="xs:date"/>

When defining XSD elements, we use default if it's a default element, fixed if it's fixed:

<xs:element name="language" type="xs:string" fixed="Turkish"/>
<xs:element name="edition" type="xs:integer" default="1"/>

Attribute Definition

To define attributes:

<xs:attribute name="attribute-name" type="attribute-type"/>

Attribute Types:

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

Let's use our library example again. Example XML:

<xml version="1.0"?>
<book isbn="9759954949">Yunus Emre Divani</book>
<author role="Editor">Selim Yagmur</author>

Let's define the attributes with XSD:

<xs:attribute name="isbn" type="xs:integer"/>
<xs:attribute name="role" type="xs:string"/>

Restrictions

The general structure for restricting values within elements:

<xs:element name="element-name">
	<xs:simpletype>
		<xs:restriction base="element-type">
			<!-- Restrictions -->
		</xs:restriction>
	</xs:simpletype>
</xs:element>

To determine the minimum and maximum edition values, we use minInclusive and maxInclusive:

<xs:element name="edition">
	<xs:simpletype>
		<xs:restriction base="xs:integer">
			<xs:minInclusive value="1"/>
			<xs:maxInclusive value="500"/>
		</xs:restriction>
	</xs:simpletype>
</xs:element>

We set the edition element to minimum 1 and maximum 500.

<xs:element name="role">
	<xs:simpletype>
		<xs:restriction base="xs:string">
			<xs:enumeration value="author"/>
			<xs:enumeration value="translator"/>
			<xs:enumeration value="editor"/>
		</xs:restriction>
	</xs:simpletype>
</xs:element>

We set 3 options for the author's role element. To write one restriction and use it in many places, we need to convert the restrictions to a type:

<!-- When defining an element, you need to write the name of the restriction you'll create as the type -->
<xs:element name="element-name" type="restriction-name"/>
<xs:simpletype name="restriction-name">
	 <xs:restriction base="xs:string">
			<!-- Restrictions -->
	 </xs:restriction>
</xs:simpletype>
<!-- Example -->
<xs:element name="role" type="roleRestriction"/>
<xs:simpleType name="roleRestriction">
	 <xs:restriction base="xs:string">
			<xs:enumeration value="author"/>
			<xs:enumeration value="translator"/>
			<xs:enumeration value="editor"/>
	 </xs:restriction>
</xs:simpleType>

You can define patterns:

<xs:element name="element-name">
	<xs:simpletype>
		<xs:restriction base="xs:string">
			<xs:pattern value="pattern"/>
		</xs:restriction>
	</xs:simpletype>
</xs:element>

Let's define a pattern where the first character is lowercase and the others are uppercase:

<xs:element name="name">
	<xs:simpletype>
		<xs:restriction base="xs:string">
			<xs:pattern value="[a-z]([A-Z])*"/>
		</xs:restriction>
	</xs:simpletype>
</xs:element>

Patterns and their explanations:

<xs:element name="name">
	<xs:simpletype>
		<xs:restriction base="xs:string">
			<xs:pattern value="[a-z]"/> <!-- Single character and lowercase letters -->
			<xs:pattern value="[A-Z]"/> <!-- Single character and uppercase letters -->
			<xs:pattern value="[a-zA-Z]"/> <!-- Single character and lowercase or uppercase letters -->
			<xs:pattern value="[a-zA-Z][a-zA-Z]"/> <!-- Two characters and lowercase or uppercase letters -->
			<xs:pattern value="xyz"/> <!-- One character from x, y, z letters -->
			<xs:pattern value="[0-9]"/> <!-- A digit from 0 to 9 -->
			<xs:pattern value="([a-z])*"/> <!-- Any number of lowercase letters -->
			<xs:pattern value="([a-z])+"/> <!-- At least one or more letters -->
			<xs:pattern value="john|jane"/> <!-- john or jane -->
			<xs:pattern value="([a-z])*"/> <!-- Any number of lowercase letters -->
		</xs:restriction>
	</xs:simpletype>
</xs:element>

Try the above patterns one by one. For whitespace operations:

<xs:element name="name">
	<xs:simpletype>
		<xs:restriction base="xs:string">
			<xs:whiteSpace value="preserve"/> <!-- #x9, #xA, #xD doesn't make any changes -->
			<xs:whiteSpace value="replace"/> <!-- Replaces #x9(tab), #xA, #xD characters with #x20(space) -->
			<xs:whiteSpace value="collapse"/> <!-- Removes whitespace -->
		</xs:restriction>
	</xs:simpletype>
</xs:element>

For character length operations:

<xs:element name="name">
	<xs:simpletype>
		<xs:restriction base="xs:string">
			<xs:length value="5"/> <!-- Specifies how many characters there should be -->
			<xs:minLength value="1"/> <!-- Specifies minimum number of characters -->
			<xs:maxLength value="10"/> <!-- Specifies maximum number of characters -->
		</xs:restriction>
	</xs:simpletype>
</xs:element>

Types used in restrictions:

  • enumeration: accepted values
  • fractionDigits: 2.4, here we determine how many digits after the decimal point
  • length: we determine how many characters it will take
  • maxExclusive: we determine the maximum numerical value (must be less than the specified value)
  • maxInclusive: we determine the maximum numerical value (must be less than or equal to the specified value)
  • maxLength: we determine the maximum number of characters
  • minExclusive: we determine the minimum numerical value (must be greater than the specified value)
  • minInclusive: we determine the minimum numerical value (must be greater than or equal to the specified value)
  • minLength: we determine the minimum number of characters
  • pattern: we define a pattern and make it accept values accordingly
  • totalDigits: we determine how many total characters there should be
  • whiteSpace: we determine situations related to whitespace