This document provides an introduction to XML Schema, including:
- XML Schema defines the structure and legal elements of an XML document, similar to a DTD.
- XML Schema supports data types, which makes validating data easier.
- XML Schemas are written in XML syntax, allowing them to be edited, parsed, and manipulated using XML tools.
- XML Schemas help secure data communication by ensuring mutual understanding of data formats.
- XML Schemas are extensible and can reference other schemas.
- Examples are provided of a simple XML document and its corresponding XML Schema.
3. Introduction to XML Schema
XML Schema is an XML-based alternative to
DTD.
An XML schema describes the structure of an
XML document.
The XML Schema language is also referred to
as XML Schema Definition (XSD).
3
4. Purpose of an XML Schema
The purpose of an XML Schema is to define the legal building
blocks of an XML document, just like a DTD.
defines elements that can appear in a document
defines attributes that can appear in a document
defines which elements are child elements
defines the order of child elements
defines the number of child elements
defines whether an element is empty or can include text
defines data types for elements and attributes
defines default and fixed values for elements and attributes
4
5. XML Schemas Support Data
Types
One of the greatest strength of XML Schemas is the support for
data types.
With support for data types:
It is easier to describe allowable document content
It is easier to validate the correctness of data
It is easier to work with data from a database
It is easier to define data facets (restrictions on data)
It is easier to define data patterns (data formats)
It is easier to convert data between different data types
5
6. XML Schemas use XML Syntax
Another great strength about XML Schemas is that they are written
in XML.Some benefits of that XML Schemas are written in XML:
You don't have to learn a new language
You can use your XML editor to edit your Schema files
You can use your XML parser to parse your Schema files
You can manipulate your Schema with the XML DOM
You can transform your Schema with XSLT
6
7. XML Schemas Secure Data
Communication
With XML Schemas, the sender can describe the data in a way
that the receiver will understand.
A date like: "03-11-2004" will, in some countries, be interpreted
as 3.November and in other countries as 11.March.
However, an XML element with a data type like this:
<date type="date">2004-03-11</date>
ensures a mutual understanding of the content, because the XML
data type "date" requires the format "YYYY-MM-DD".
7
8. XML Schemas are Extensible
XML Schemas are extensible, because they are written in XML.
With an extensible Schema definition you can:
Reuse your Schema in other Schemas
Create your own data types derived from the standard types
Reference multiple schemas in the same document
8
9. A Simple XML Document
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
9
12. xmlns:xs="http://www.w3.org/2001/XMLSchema"
indicates that the elements and data types used in the schema
come from the "http://www.w3.org/2001/XMLSchema"
namespace. It also specifies that the elements and data types
that come from the
"http://www.w3.org/2001/XMLSchema" namespace
should be prefixed with xs:
12
13. targetNamespace="http://www.w3schools.com" indicates
that the elements defined by this schema (note, to, from,
heading, body.) come from the
"http://www.w3schools.com" namespace.
This fragment:
xmlns="http://www.w3schools.com" indicates that the
default namespace is "http://www.w3schools.com".
13
14. XML Schemas define the elements of your XML
files.
A simple element is an XML element that contains
only text. It cannot contain any other elements or
attributes.
Simple elements cannot have attributes. If an element has
attributes, it is considered to be of a complex type. But the
attribute itself is always declared as a simple type
<xs:attribute name="xxx" type="yyy"/>
where xxx is the name of the attribute and yyy specifies the
data type of the attribute.
14
15. XML Schema has a lot of built-in data types. The most
common types are:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
15