XML Schema - An XML schema describes the structure of an XML document.
XML schema language also referred as XML schema definition(XSD).
XSD is XML based alternative of DTD(Data Type Definition).
Basics
1. Defines elements of document.
2. Defines attributes of document.
3. Defines which elements are child elements.
4. Defines no. of child elements and order of child elements.
5. Defines an element is empty or can have text.
6. Defines data types for elements and attributes.
7. Defines Default and fixed values for elements and attributes.
8. W3C recommendation.
XSDs are successor of DTDs because
1. XSDs are richer, more powerful and extensible in future.
2. Written in XML.
3. Support data types.
4. Support namespaces.
Why XSDs are recommended
1. XML format - Platform independent.
2. Extensible - Own data types can be created based on standard data types, other schemas can be referenced in a schema, a document can have references of multiple schemas.
3. Support data types - Can be define allowable contents, makes data type conversion easy.
4. Secure data communication - Both ends having data structure.
Example
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>
DTD(Document Type Definition) for XML document
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
XSD for XML document
<?xml version="1.0"?>
<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>
note is complex type element because it contains other elements. to, from, heading and body are simple elements because they are not contains other elements.
XSD description
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>
1. <schema></schema> is the root element of every XML schema.
2. xmlns:xs="http://www.w3.org/2001/XMLSchema" indicates that elements and data types used in this schema come from this namespace. It also indicate that elements and data types come from this namespace should be prefixed with xs: .
3. 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.
4. xmlns="http://www.w3schools.com" indicates that this is default namespace.
5. elementFormDefault="qualified" indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
Referencing XML schema in a XML document-
<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
* xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.
* xsi:schemaLocation="http://www.w3schools.com note.xsd" you can use the schemaLocation attribute. This attribute has two values, separated by a space. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace:
Simple element - An element that contains only text(of any data type come from namespaces or defined in XSD) not contains other elements and attributes.
<xs:element name="xxx" type="yyy"/>
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
Here are some XML elements:
<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-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"/>
Default and Fixed Values for Simple Elements
In the following example the default value is "black":
<xs:element name="color" type="xs:string" default="black"/>
In the following example the fixed value is "blue":
<xs:element name="color" type="xs:string" fixed="blue"/>
Difining Attributes
<xs:attribute name="xxx" type="yyy"/>
type is data type.
Attribute lang <lastname lang="EN">Smith</lastname>
Definition of attribute lang <xs:attribute name="lang" type="xs:string"/>
Attributes are optional by default. To specify that the attribute is required, use the "use" attribute.
<xs:attribute name="lang" type="xs:string" use="required"/>
Complex element - An element that contain other elements or have any attribute.
Defining complex elements
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
1. The "employee" element can be declared directly by naming the element, like this:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Child elements, "firstname" and "lastname", are surrounded by the <sequence> indicator. This means that the child elements must appear in the same order as they are declared.
2. The "employee" element can have a type attribute that refers to the name of the complex type to use:
<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
XML schema language also referred as XML schema definition(XSD).
XSD is XML based alternative of DTD(Data Type Definition).
Basics
1. Defines elements of document.
2. Defines attributes of document.
3. Defines which elements are child elements.
4. Defines no. of child elements and order of child elements.
5. Defines an element is empty or can have text.
6. Defines data types for elements and attributes.
7. Defines Default and fixed values for elements and attributes.
8. W3C recommendation.
XSDs are successor of DTDs because
1. XSDs are richer, more powerful and extensible in future.
2. Written in XML.
3. Support data types.
4. Support namespaces.
Why XSDs are recommended
1. XML format - Platform independent.
2. Extensible - Own data types can be created based on standard data types, other schemas can be referenced in a schema, a document can have references of multiple schemas.
3. Support data types - Can be define allowable contents, makes data type conversion easy.
4. Secure data communication - Both ends having data structure.
Example
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>
DTD(Document Type Definition) for XML document
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
XSD for XML document
<?xml version="1.0"?>
<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>
note is complex type element because it contains other elements. to, from, heading and body are simple elements because they are not contains other elements.
XSD description
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>
1. <schema></schema> is the root element of every XML schema.
2. xmlns:xs="http://www.w3.org/2001/XMLSchema" indicates that elements and data types used in this schema come from this namespace. It also indicate that elements and data types come from this namespace should be prefixed with xs: .
3. 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.
4. xmlns="http://www.w3schools.com" indicates that this is default namespace.
5. elementFormDefault="qualified" indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
Referencing XML schema in a XML document-
<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
* xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.
* xsi:schemaLocation="http://www.w3schools.com note.xsd" you can use the schemaLocation attribute. This attribute has two values, separated by a space. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace:
Simple element - An element that contains only text(of any data type come from namespaces or defined in XSD) not contains other elements and attributes.
<xs:element name="xxx" type="yyy"/>
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
Here are some XML elements:
<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-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"/>
Default and Fixed Values for Simple Elements
In the following example the default value is "black":
<xs:element name="color" type="xs:string" default="black"/>
In the following example the fixed value is "blue":
<xs:element name="color" type="xs:string" fixed="blue"/>
Difining Attributes
<xs:attribute name="xxx" type="yyy"/>
type is data type.
Attribute lang <lastname lang="EN">Smith</lastname>
Definition of attribute lang <xs:attribute name="lang" type="xs:string"/>
Attributes are optional by default. To specify that the attribute is required, use the "use" attribute.
<xs:attribute name="lang" type="xs:string" use="required"/>
Complex element - An element that contain other elements or have any attribute.
Defining complex elements
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
1. The "employee" element can be declared directly by naming the element, like this:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Child elements, "firstname" and "lastname", are surrounded by the <sequence> indicator. This means that the child elements must appear in the same order as they are declared.
2. The "employee" element can have a type attribute that refers to the name of the complex type to use:
<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
No comments:
Post a Comment