Unit 3 - XML
Unit 3 - XML
(XML)
Overview
• Introduction to XML
• XML Namespaces
• Document Type Definition(DTD)
• XML Schemas
• Transforming XML into XSLT
Introduction to XML
• Xml (eXtensible Markup Language) is a mark
up language.
• XML is designed to store and transport data.
• Xml was released in late 90’s. it was created to
provide an easy to use and store self
describing data.
• XML became a W3C Recommendation on
February 10, 1998.
Introduction to XML
• XML is not a replacement for HTML.
• XML is designed to be self-descriptive.
• XML is designed to carry data, not to display
data.
• XML tags are not predefined. You must define
your own tags.
• XML is platform independent and language
independent.
Introduction to XML
Use of XML?
– It allows you to invent your own tags
– XML documents can be easily parsed
– XML is portable
Example :
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Features and Advantages of XML
<tiger>bengal tiger</tiger>
<tiger> glucose tiger </tiger>
Namespaces
note.dtd
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
XML SCHEMAS Documents
• XML Schema is an XML-based (and more powerful) alternative
to DTD. It Do not use EBNF grammar
• The XML Schema language is also referred to as XML
Schema Definition (XSD).
• One of the greatest strength of XML Schemas is the
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 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
XML SCHEMAS Documents
• XML Schemas is that are written in XML so you
don't have to learn a new language.
• 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
• XML Schemas Secure Data Communication.
• Well-formed documents can also contain errors,
and those errors can have serious consequences.
With XML Schemas, most of the errors can be
caught by your validating software.
XML Schema Documents
• Root element schema
– Contains elements that define the XML
document structure
– targetNamespace
• Namespace of XML vocabulary the schema defines
– element tag
• Defines element to be included in XML document
structure
– name and type attributes
• Specify element’s name and data type respectively
– Built-in simple types
• date, int, double, time, etc
Simple example of a XML Schema
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:element name = "contact">
Complex types
<xs:complexType>
<xs:sequence>
<xs:element name = "name" type = "xs:string" />
<xs:element name = "company" type = "xs:string" />
<xs:element name = "phone" type = "xs:int" />
</xs:sequence> Simple types
</xs:complexType>
</xs:element>
</xs:schema>
XML schema element Definition Types
• Two categories of data types
1. Simple types
• Cannot contain attributes or child elements
• Can be used only in the context of the text.
• Some of the predefined simple types are: xs:integer,
xs:boolean, xs:string, xs:date.
2. Complex types
• May contain attributes and child elements
• This allows you to specify which child elements an
element can contain and to provide some structure
within your XML documents.
<?xml version="1.0"?>
<note
note.xml
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLS
chema-instance"
xsi:schemaLocation="http://www.w3school
s.com note.xsd">
<?xml version="1.0"?>
<to>Tove</to> <xs:schema
<from>Jani</from> xmlns:xs="http://www.w3.org/2001/XMLSchema"
<heading>Reminder</heading> targetNamespace="http://www.w3schools.com"
<body>Don't forget me this xmlns="http://www.w3schools.com"
weekend!</body> elementFormDefault="qualified">
</note>
<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"/>
note.xsd <xs:element name="body" type="xs:string"/>
</xs:sequence>
that defines the elements of the XML </xs:complexType>
document above ("note.xml"): </xs:element>
</xs:schema>
Difference Between XML Schema and DTD