XML documents can have a reference to a DTD or an XML Schema.
Look at this simple XML document called "note.xml":
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
This is a simple DTD file called "note.dtd" that defines the elements of the XML document above ("note.xml"):
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
Line 1 defines the note element to have four elements: "to, from, heading, body". Line 2-5 defines the to element to be of the type "#PCDATA", the from element to be of the type "#PCDATA", and so on...
This is a simple XML Schema file called "note.xsd" that defines the elements of the XML document above ("note.xml"):
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The note element is said to be of a complex type because it contains other elements. The other elements (to, from, heading, body) are said to be simple types because they do not contain other elements. You will learn more about simple and complex types in the following chapters.
This XML document has a reference to a DTD:
<!DOCTYPE note SYSTEM
"http://www.w3schools.com/dtd/note.dtd">
This XML document has a reference to an XML Schema:
<note
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.w3schools.com/schema/note.xsd">
The <schema> element is the root element of every XML Schema!
The <schema> element is the root element of every XML Schema:
<xs:schema>
...
The <schema> element may contain some attributes. A schema declaration often looks something like this:
The following fragment:
xmlns:xs="http://www.w3.org/2001/XMLSchema"
indicates that the elements and data types used in the schema (schema, element, complexType, sequence, string, boolean, etc.) 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: !!
This fragment:
indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace.
indicates that the default namespace is "http://www.w3schools.com".
elementFormDefault="qualified"
indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
<note xmlns="http://www.w3schools.com"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
specifies the default namespace declaration. This declaration tells the schema-validator that all the elements used in this XML document are declared in the "http://www.w3schools.com" namespace.
Once you have the XML Schema Instance namespace available:
you can use the schemaLocation attribute. This attribute has two values. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace:
xsi:schemaLocation="http://www.w3schools.com note.xsd"
XML Schemas define the elements of your XML files.
A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes.
However, the "only text" restriction is quite misleading. The text can be of many different types. It can be one of the types that are included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself.
You can also add restrictions (facets) to a data type in order to limit its content, and you can require the data to match a defined pattern.
The syntax for defining a simple element is:
<xs:element name="xxx" type="yyy"/>
where xxx is the name of the element and yyy is the data type of the element.
Here are some XML elements:
<lastname>Refsnes</lastname>
<age>34</age>
<dateborn>1968-03-27</dateborn>
And here are the corresponding simple element definitions:
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
XML Schema has a lot of built-in data types. Here is a list of the most common types:
§ xs:string
§ xs:decimal
§ xs:integer
§ xs:boolean
§ xs:date
§ xs:time
Simple elements can have a default value OR a fixed value set.
A default value is automatically assigned to the element when no other value is specified. In the following example the default value is "red":
<xs:element name="color" type="xs:string" default="red"/>
A fixed value is also automatically assigned to the element. You cannot specify another value. In the following example the fixed value is "red":
<xs:element name="color" type="xs:string" fixed="red"/>
All attributes are declared as simple types.
Only complex elements can have attributes!
Simple elements cannot have attributes. If an element has attributes, it is considered to be of complex type. But the attribute itself is always declared as a simple type. This means that an element with attributes always has a complex type definition.
The syntax for defining an attribute is:
<xs:attribute name="xxx" type="yyy"/>
where xxx is the name of the attribute and yyy is the data type of the attribute.
Here are an XML element with an attribute:
<lastname lang="EN">Smith</lastname>
And here are a corresponding simple attribute definition:
<xs:attribute name="lang" type="xs:string"/>
Attributes can have a default value OR a fixed value specified.
A default value is automatically assigned to the attribute when no other value is specified. In the following example the default value is "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
A fixed value is also automatically assigned to the attribute. You cannot specify another value. In the following example the fixed value is "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
All attributes are optional by default. To explicitly specify that the attribute is optional, use the "use" attribute:
<xs:attribute name="lang" type="xs:string" use="optional"/>
To make an attribute required:
<xs:attribute name="lang" type="xs:string" use="required"/>
When an XML element or attribute has a type defined, it puts a restriction for the element's or attribute's content. If an XML element is of type "xs:date" and contains a string like "Hello Mother", the element will not validate.
But, there is more... with XML Schemas, you can add your own restrictions to your XML elements and attributes. These restrictions are called facets. You can read more about facets in the next chapter.
Restrictions are used to control acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
This example defines an element called "age" with a restriction. The value of age can NOT be lower than 0 or greater than 100:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer"&...
Phoob