0% found this document useful (0 votes)
4 views65 pages

L07 XML DTD XSD

Uploaded by

info.bptrades
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views65 pages

L07 XML DTD XSD

Uploaded by

info.bptrades
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

CSIT128/828

XML - DTD - XSD


Joseph Tonien
School of Computing and Information Technology
University of Wollongong
● 56A72Q267.85

● <cust>56A</cust>
● <prod>72Q</prod>
● <qty>26</qty>
● <price>7.85</price>
XML

EXtensible Markup Language

● XML is a markup language much like HTML

● XML is a software- and hardware-independent tool for storing and


transporting data.

● XML separates data from presentation.

● File extension is .xml

<?xml version="1.0" ?>


<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<mobile>0211223344</mobile>
</student>
XML

● HTML tags are predefined.

● XML tags are defined by user.

● Using XML Document Type Definition (DTD), or XML Schema Definition (XSD),
different parties can agree on a standard XML format for interchanging data.

● Another popular format for interchanging data is JavaScript Object Notation


(JSON)

{
"firstName": "John",
"lastName": "Smith",
"email": "[email protected]",
"mobile": "0211223344"
}

● In most web applications, XML and JSON are used to store or transport data,
while HTML and XSLT are used to transform and display the data.
XML:
The first example of XML:

<?xml version="1.0" ?>


<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<mobile>0211223344</mobile>
</student>
XML: XML declaration
<?xml version="1.0" ?> XML declaration
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<mobile>0211223344</mobile>
</student>
● The XML declaration is optional and it must come first in the document.
● The XML declaration identifies the document as being XML. Even though it is
optional, all XML documents should begin with an XML declaration.
● The XML declaration must be situated at the first position of the first line in the
XML document.
○ Do not start an XML file with a blank line!!!
● Syntax for the XML declaration:
<?xml version="version_number"
encoding="encoding_declaration"
standalone="standalone_status" ?>
XML: root element
<?xml version="1.0" encoding="UTF-8" ?>
<student> root element
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<mobile>0211223344</mobile>
</student>

● An XML document must contain one root element that is the parent of all
other elements

<rootElement>
<child>
<subchild>.....</subchild>
</child>
</rootElement>
XML: root element
This is NOT a well-formed XML document because it has no root element

<?xml version="1.0" encoding="UTF-8"?>


<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
XML: root element
This is a well-formed XML document because it has a root element

<?xml version="1.0" encoding="UTF-8"?>


<studentList>
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>
XML: element
<tag attribute1="..." attribute2="...">

</tag>

● An XML element is everything from (including) the element's start tag to


(including) the element's end tag.

<?xml version="1.0" encoding="UTF-8"?>


<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
<person staffDbId="-1" operation="add">
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<mobile>0244556677</mobile>
</person>
</dailyTransaction>

Where is the dailyTransaction element?


Where is a person element?
Where is a mobile element?
XML: element
XML tags are case sensitive.

The tag <student> is different from the tag <STUDENT>

Common naming convention for XML tags

<student_list>
...
</student_list>

or

<studentList>
...
</studentList>
XML: attribute
<tag attribute1="..." attribute2="...">

</tag>

● XML attributes are used to describe XML elements, or to provide additional


information about elements.

<?xml version="1.0" encoding="UTF-8"?>


<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
<person staffDbId="-1" operation="add">
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<mobile>0244556677</mobile>
</person>
</dailyTransaction>

Does the dailyTransaction element has attributes?


Does a person element has attributes?
Does a mobile element has attributes?
XML: attribute
In XML, the attribute values must always be quoted (either by single quote or
double quote):

<dailyTransaction date='24/02/2015'>
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
</dailyTransaction>
XML: relationship between elements

<parent>
<child>
<subchild>.....</subchild>
</child>
</parent>

● An XML tree starts at a root element and branches from the root to child
elements.

● The terms parent, child, and sibling are used to describe the relationships
between elements.
○ Parent have children. Children have parents.
○ Siblings are children on the same level
XML: attribute vs child element
Any attribute can be defined as a child element.

For example, instead of using gender as an attribute


<person gender="M">
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</person>

we can define gender as a child element of person


<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<gender>M</gender>
</person>

This contains the same information.


XML: attribute vs child element
Any attribute can be defined as a child element.

For example, attributes staffDbId and operation


<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>

can become child elements


<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
<staffDbId>103</staffDbId>
<operation>update</operation>
</person>

This contains the same information.


XML: attribute vs child element
Any attribute can be defined as a child element, so when should we use attribute
and when should we use element?

Metadata (data about data) should be stored as attributes, and the data itself
should be stored as elements.

<person gender="M">
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</person>

<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<gender>M</gender> this is better
</person>
XML: attribute vs child element
Any attribute can be defined as a child element, so when should we use attribute
and when should we use element?

Metadata (data about data) should be stored as attributes, and the data itself
should be stored as elements.

<person staffDbId="103" operation="update">


<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>

<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
<staffDbId>103</staffDbId>
<operation>update</operation> this is better
</person>
XML: empty element and self-closing tag
In HTML, some elements might work well, even with a missing closing tag:
<br>
<hr>
<p>
<input ...>

In XML, all elements must have a closing tag:


<student>
...
</student>

An element with no content is called an empty element:


<emptyElement></emptyElement>

We can use self-closing tag for an empty element:


<emptyElement />
XML: nested rule
In HTML, some elements might not be nested properly:
<b><i>This text is bold and italic</b></i>

In XML, all elements must be properly nested:

<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>
XML: entity reference
If we place a character like < inside an XML element, it will generate an error.
In this case, we need to use the entity reference &lt;

Entity references

&lt; < less than


&gt; > greater than
&amp; & ampersand
&apos; ' apostrophe
&quot; " quotation mark
XML: comments
Comments in XML:

<!-- this is a comment -->


DTD

● XML Document Type Definition commonly known as DTD is a way to define


the legal building blocks of an XML document. It defines the document
structure with a list of legal elements and attributes.

● Using a DTD, different parties can agree on a standard XML format for
interchanging data.

● We can check whether an XML document conforms to a DTD or not.

● File extension is .dtd


DTD

The DTD can be declared inside the XML file, or it can be defined in a separate
file:

● Internal DTD

● External DTD
DTD: internal DTD

The following DTD is declared inside the XML file:

<?xml version="1.0" standalone="yes" ?>


<!DOCTYPE student [
<!ELEMENT student (firstName,lastName,email,mobile)>
<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT mobile (#PCDATA)>
]>
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<mobile>0211223344</mobile>
</student>
DTD: external DTD

DTD is declared outside the XML file:

<?xml version="1.0" standalone="no" ?>


<!DOCTYPE student SYSTEM "student.dtd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<mobile>0211223344</mobile>
</student>

The content of student.dtd

<!ELEMENT student (firstName,lastName,email,mobile)>


<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT mobile (#PCDATA)>
DTD: internal DTD

The following DTD is declared inside the XML file:


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE studentList [
<!ELEMENT studentList (student*)>
<!ELEMENT student (firstName,lastName,email)>
<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>
<!ELEMENT email (#PCDATA)>
]>
<studentList>
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>
DTD: external DTD

DTD is declared outside the XML file:


<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE studentList SYSTEM "studentList.dtd">
<studentList>
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

The content of studentList.dtd


<!ELEMENT studentList (student*)>
<!ELEMENT student (firstName,lastName,email)>
<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>
<!ELEMENT email (#PCDATA)>
DTD: external DTD

DTD is declared outside the XML file:


<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE studentList SYSTEM "studentList.dtd">
<studentList>
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

To reference it as external DTD, standalone


attribute in the XML declaration must be set as no.
This means, declaration includes information from
the external source.
DTD: Element declaration

XML elements are building blocks of an XML document.

An element is everything from the element's start tag to the element's end tag:

<firstName>John</firstName>
<lastName>Smith</lastName>

In DTD, we declare element as follows:


<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>

Here PCDATA stands for parsed character data.


DTD: Element declaration

An element can contain other elements


<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>

In DTD, we declare as follows:


<!ELEMENT student (firstName,lastName,email)>

It means, the element student contains elements firstName, lastName and


email.
DTD: Element declaration

An element can contain other elements


<studentList>
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

In DTD, we declare as follows:


<!ELEMENT studentList (student*)>

It means, the element studentList contains zero or more elements student.


DTD: Element declaration

This is the general form of element declaration:


<!ELEMENT elementName (content)>

● elementName is the element name that you are defining.


● content defines what content (if any) can go within the element
DTD: Element declaration
Element content:
<!ELEMENT elementName (child1, child2,...)>

Example:
<!ELEMENT studentList (student*)>

<!ELEMENT student (firstName,lastName,email)>

<!ELEMENT elementName (child+)> child element can occur one or


more times inside parent element

<!ELEMENT elementName (child*)> child element can occur zero or


more times inside parent element

<!ELEMENT elementName (child?)> child element can occur zero or


one time inside parent element

<!ELEMENT elementName (child1|child2)> either of child1 or child2 must


occur in inside parent element

<!ELEMENT elementName (child1,child2,child3,...)> Parent element must have


child1,child2,child3,...
appear in this order
DTD: Attribute declaration
This is the general form of attribute declaration:
<!ATTLIST elementName attributeName attributeType attributeValue>

● elementName specifies the name of the element to which the attribute applies,

● attributeName specifies the name of the attribute,

● attributeType defines the type of attributes

● attributeValue defines the attribute value


DTD: Attribute declaration
<!ATTLIST elementName attributeName attributeType attributeValue>
attributeValue

● can have a default value


<!ATTLIST elementName attributeName attributeType "default-value">

● can have a fixed value


<!ATTLIST elementName attributeName attributeType #FIXED "value">

● is required
<!ATTLIST elementName attributeName attributeType #REQUIRED>

● is implied: if the attribute has no default value, has no fixed value, and is not
required, then it must be declared as implied
<!ATTLIST elementName attributeName attributeType #IMPLIED>
DTD: Attribute declaration
<?xml version="1.0" ?>
<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
<person staffDbId="-1" operation="add">
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<mobile>0244556677</mobile>
</person>
</dailyTransaction>
CDATA = unparsed character
data which may contain
unescaped character

<!ELEMENT dailyTransaction (person*)>


<!ATTLIST dailyTransaction date CDATA #REQUIRED>
<!ELEMENT person (firstName,lastName,mobile)>
<!ATTLIST person staffDbId CDATA #REQUIRED>
<!ATTLIST person operation CDATA #REQUIRED>
<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>
<!ELEMENT mobile (#PCDATA)>
XSD

● XML Schema Definition (XSD) is another way to define the legal building
blocks of an XML document. It defines the document structure with a list of
legal elements and attributes.

● Using a XSD, different parties can agree on a standard XML format for
interchanging data.

● We can check whether an XML document conforms to a XSD or not.

● File extension is .xsd


XSD: student example
XML file:
<?xml version="1.0" ?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<mobile>0211223344</mobile>
</student>

XSD file student.xsd:


<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="student">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:schema>
XSD: student example
XML file:
<?xml version="1.0" ?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
<firstName>John</firstName>
<lastName>Smith</lastName> elements and data types used in the schema
<email>[email protected]</email> come from the namespace
<mobile>0211223344</mobile> http://www.w3.org/2001/XMLSchema
</student>

XSD file student.xsd:


<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="student">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:schema>
XSD: student example
XML file:
<?xml version="1.0" ?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
<firstName>John</firstName>
<lastName>Smith</lastName> the elements and data types that come from
<email>[email protected]</email> the namespace
<mobile>0211223344</mobile> http://www.w3.org/2001/XMLSchema
</student> should be prefixed with xsd

XSD file student.xsd:


<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="student">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:schema>
XSD: student example
XML file:
<?xml version="1.0" ?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<mobile>0211223344</mobile>
</student>

XSD file student.xsd:


<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="student">
Complex type <xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
Simple type <xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:schema>
XSD: element

XML element can be defined in XSD as 2 types:


● simpleType
● complexType

● Element contains other elements → complexType

● Element contains attributes → complexType

● Element contains NO attributes, NO elements → simpleType


XSD: complex type containing element
● Element contains other elements → complexType

<result>
<mark>85</mark>
<grade>A</grade>
</result>

<xsd:element name="result">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="mark" type="xsd:integer"/>
<xsd:element name="grade" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
XSD: complex type containing element and attribute
● Element contains other elements and attributes → complexType

<scan schedule="hourly">
<start>2018-06-20T13:00:00</start>
<finish>2018-06-20T13:01:47</finish>
<virusFound>true</virusFound>
</scan>
The attribute declarations
must always come last
<xsd:element name="scan">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="start" type="xsd:dateTime" />
<xsd:element name="finish" type="xsd:dateTime" />
<xsd:element name="virusFound" type="xsd:boolean" />
</xsd:sequence>
<xsd:attribute name="schedule" type="xsd:string" />
</xsd:complexType>
</xsd:element>
XSD: complex type containing attributes only

● Text-only element contains attributes (does not contain elements)


→ complexType

<price promotionCode="FAMILYDEAL">39.50</price>

<xsd:element name="price">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="promotionCode" type="xsd:string" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
XSD: simple type containing no element, no attribute
● Element contains no elements, no attributes → simpleType

<website>http://www.uow.edu.au/student</website>

<lastDayToEnrol>2000-03-24</lastDayToEnrol>

<favouriteColor>blue</favouriteColor>

<xsd:element name="website" type="xsd:anyURI" />

<xsd:element name="lastDayToEnrol" type="xsd:date" />

<xsd:element name="favouriteColor" type="xsd:string" />


XSD: simple type with restriction

Grade can have 4 values: A, B, C, D

<grade>B</grade>

Without restriction:
<xsd:element name="grade" type="xsd:string" />

With restriction:
<xsd:element name="grade">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="A"/>
<xsd:enumeration value="B"/>
<xsd:enumeration value="C"/>
<xsd:enumeration value="D"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
XSD: simple type with restriction

Mark can have values between 0-100

<mark>84</mark>

Without restriction:
<xsd:element name="mark" type="xsd:integer" />

With restriction:
<xsd:element name="mark">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

<?xml version="1.0" ?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
Let’s start with the root element studentList
<email>[email protected]</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

<?xml version="1.0" ?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
Let’s start with the root element studentList
<email>[email protected]</email> ● it is a complex type
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

<?xml version="1.0" ?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
Let’s start with the root element studentList
<email>[email protected]</email> ● it is a complex type
</student>
<student>
● which contains a sequence of student elements
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

<?xml version="1.0" ?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
Let’s start with the root element studentList
<email>[email protected]</email> ● it is a complex type
</student>
<student>
● which contains a sequence of student elements
<firstName>Mary</firstName> ● studentList contains zero or unlimited
<lastName>Jane</lastName>
<email>[email protected]</email>
number of student elements
</student>
</studentList>

<?xml version="1.0" ?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
The element student is also a complex type
<email>[email protected]</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

<?xml version="1.0" ?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
The element student is also a complex type
<email>[email protected]</email> ● which contains a sequence of elements
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

<?xml version="1.0" ?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
The element student is also a complex type
<email>[email protected]</email> ● which contains a sequence of elements:
</student>
<student>
firstName, lastName, email
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

<?xml version="1.0" ?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
firstName, lastName, email elements are all
<email>[email protected]</email> simple type
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>

<?xml version="1.0" ?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD: dailyTransaction example
<?xml version="1.0" ?>
<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
<person staffDbId="-1" operation="add">
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<mobile>0244556677</mobile>
</person>
</dailyTransaction>

complexType: dailyTransaction, person

simpleType: firstName, lastName, mobile


XSD: dailyTransaction example
<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
...
</person>
<person staffDbId="-1" operation="add">
...
</person>
</dailyTransaction>

Start with the root element dailyTransaction:

<?xml version="1.0" ?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="dailyTransaction">
<xsd:complexType>
...
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD: dailyTransaction example
<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
...
</person>
<person staffDbId="-1" operation="add">
...
</person>
</dailyTransaction>

The root element dailyTransaction contains a sequence of


person elements and has attribute date

<xsd:element name="dailyTransaction">
<xsd:complexType>
<xsd:sequence>
...
</xsd:sequence>
<xsd:attribute name="date" type="xsd:string" />
</xsd:complexType>
</xsd:element>
XSD: dailyTransaction example
<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
...
</person>
<person staffDbId="-1" operation="add">
...
</person>
</dailyTransaction>

The root element dailyTransaction contains a sequence of


person elements and has attribute date

<xsd:element name="dailyTransaction">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="person" minOccurs="0" maxOccurs="unbounded">
...
</xsd:element>
</xsd:sequence>
<xsd:attribute name="date" type="xsd:string" />
</xsd:complexType>
</xsd:element>
XSD: dailyTransaction example
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>

The element person contains:


● elements: firstName, lastName, mobile
● attributes: staffDbId, operation

<xsd:element name="person" minOccurs="0" maxOccurs="unbounded">


<xsd:complexType>
<xsd:sequence>
...
</xsd:sequence>
<xsd:attribute name="staffDbId" type="xsd:integer" />
<xsd:attribute name="operation" type="xsd:string" />
</xsd:complexType>
</xsd:element>
XSD: dailyTransaction example
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>

The element person contains:


● elements: firstName, lastName, mobile
● attributes: staffDbId, operation

<xsd:element name="person" minOccurs="0" maxOccurs="unbounded">


<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="staffDbId" type="xsd:integer" />
<xsd:attribute name="operation" type="xsd:string" />
</xsd:complexType>
</xsd:element>
References

● XML Tutorial: http://www.w3schools.com/xml


● DTD Tutorial: https://www.w3schools.com/xml/xml_dtd_intro.asp
● XSD Tutorial: https://www.w3schools.com/xml/schema_intro.asp
● DTD Reference: https://msdn.microsoft.com/en-
us/library/ms256469(v=vs.110).aspx
● XSD Reference: https://msdn.microsoft.com/en-
us/library/ms256235(v=vs.110).aspx

You might also like