Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Web Technology
(NCS-504)
Prepared By
Mr.Abhishek Kesharwani
Assistant Professor,UCER Naini,Allahabad
XML Schema
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
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
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
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
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
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
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
An XML Schema
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNam
espace="http://www.w3schools.com"xmlns="http://www.w3
schools.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>10
</xs:complexType>
</xs:element>
</xs:schema>
The <schema> Element
The <schema> element is the root element of every XML Schema
<?xml version="1.0"?>
<xs:schema>......</xs:schema>
11
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
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
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
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

More Related Content

uptu web technology unit 2 Xml2

  • 1. Web Technology (NCS-504) Prepared By Mr.Abhishek Kesharwani Assistant Professor,UCER Naini,Allahabad
  • 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
  • 10. An XML Schema <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNam espace="http://www.w3schools.com"xmlns="http://www.w3 schools.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>10
  • 11. </xs:complexType> </xs:element> </xs:schema> The <schema> Element The <schema> element is the root element of every XML Schema <?xml version="1.0"?> <xs:schema>......</xs:schema> 11
  • 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