xml sasidhar

52
K.Sasidhar What is XML? What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data XML tags are not predefined. You must define your own tags XML is designed to be self-descriptive XML is a W3C Recommendation

Upload: sasidhar-kothuru

Post on 16-Jul-2015

182 views

Category:

Technology


1 download

TRANSCRIPT

K.Sasidhar

What is XML?What is XML?

XML stands for EXtensible Markup Language

XML is a markup language much like HTML

XML was designed to carry data, not to display data

XML tags are not predefined. You must define your own tags

XML is designed to be self-descriptive XML is a W3C Recommendation

K.Sasidhar

The Difference Between XML and The Difference Between XML and HTMLHTML

XML is not a replacement for HTML. XML and HTML were designed with

different goals: XML was designed to transport and

store data, with focus on what data is HTML was designed to display data,

with focus on how data looks HTML is about displaying information,

while XML is about carrying information. XML was created to structure, store,

and transport information. XML language has no predefined tags

K.Sasidhar

Differences continue….Differences continue….

XML is used to transport data, while HTML is used to format and display the data.

XML is a software- and hardware-independent tool for carrying information.

With XML You Invent Your Own Tags XML is a W3C Recommendation

K.Sasidhar

Facts of XML Facts of XML

XML Separates Data from HTMLXML Separates Data from HTML If you need to display dynamic data in your If you need to display dynamic data in your

HTML document, it will take a lot of work to HTML document, it will take a lot of work to edit the HTML each time the data changes.edit the HTML each time the data changes.

With XML, data can be stored in separate With XML, data can be stored in separate XML files. This way you can concentrate on XML files. This way you can concentrate on using HTML/CSS for display and layout, using HTML/CSS for display and layout, and be sure that changes in the underlying and be sure that changes in the underlying data will not require any changes to the data will not require any changes to the HTML.HTML.

With a few lines of JavaScript code, you With a few lines of JavaScript code, you can read an external XML file and update can read an external XML file and update the data content of your web page.the data content of your web page.

K.Sasidhar

Facts of XMLFacts of XML

XML Simplifies Data Sharing XML data is stored in plain text format.

This provides a software- and hardware-independent way of storing data.

This makes it much easier to create data that can be shared by different applications.

XML Simplifies Data Transport One of the most time-consuming

challenges for developers is to exchange data between incompatible systems over the Internet.

K.Sasidhar

FACTS OF XMLFACTS OF XML

XML Simplifies Platform ChangesXML Simplifies Platform Changes XML Makes Your Data More XML Makes Your Data More

AvailableAvailable XML is a meta mark up language XML is a meta mark up language

that specifies rules for creating that specifies rules for creating markup languagesmarkup languages

K.Sasidhar

XML DOCUMENT CONTENTXML DOCUMENT CONTENT

XML Document composed of 1. Declarations (dtd reference) 2. Elements 3. Comments 4. Entities (Pre-defined, Custom

– defined, character)

K.Sasidhar

Root StructureRoot Structure

K.Sasidhar

XML SYNTAXXML SYNTAX

Syntax is two levels.Syntax is two levels. Low level syntax that impose Low level syntax that impose

rules on all xml documents.rules on all xml documents.

Other level is either document Other level is either document type definition (DTD) or XML type definition (DTD) or XML schemas.schemas.

K.Sasidhar

XML Syntax Continues….XML Syntax Continues….

DTDs and schemas specify the DTDs and schemas specify the set of tags and attributes that set of tags and attributes that can appear in a particular can appear in a particular document or collection of document or collection of documents.documents.

Also specify the order and Also specify the order and arrangement in which they can arrangement in which they can appear.appear.

K.Sasidhar

XML StatementsXML Statements

ElementsElements Markup declarations are Markup declarations are

instructions to the xml parser, instructions to the xml parser, and processing instructions.and processing instructions.

K.Sasidhar

XML Program structureXML Program structure

XML declarationXML declaration -- identifies the document as XML and provides the -- identifies the document as XML and provides the

version number of XML.version number of XML.

CommentsComments Same as HTMLSame as HTML

XML namesXML names Names are used to name elements and attributes.Names are used to name elements and attributes. Names are case sensitiveNames are case sensitive

K.Sasidhar

ElementsElements

Root elementRoot element Xml document defines a single root element, Xml document defines a single root element,

opening tag and must appear on the first line of the opening tag and must appear on the first line of the code.code.

All other elements must be nested inside the root All other elements must be nested inside the root element.element.

K.Sasidhar

EX. PROGRAMEX. PROGRAM <?xml version = “1.0” encoding= “utf-8”?><?xml version = “1.0” encoding= “utf-8”?> <ad><ad> <year> 2012</year><year> 2012</year> <make> Maruthi </make><make> Maruthi </make> <model> Alto Lxi </model><model> Alto Lxi </model> <color> blue</color><color> blue</color> <location><location> <city> hyderabad </city><city> hyderabad </city> <state> AP </state><state> AP </state> </location></location> </ad></ad>

K.Sasidhar

Document Type Definitions

DTD is a set of structural rules called declarations.

DTD Specify the position of a set of elements.

DTD Provides entity definitions also. DTDs are used when the same tag set

definition is used by a collection of documents, perhaps by a collection of users, and the documents must have a consistent and uniform structure.

K.Sasidhar

DTD

DTD that embedded with XML is called internal DTD.

DTD stored in a separate file is called external DTD.

External DTD allows use with more than one XML document, and are preferable.

DTD defines the document structure with a list of legal elements and attributes.

K.Sasidhar

Internal DTD Example

<?xml version="1.0"?><!DOCTYPE note [<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>]><note><to>clerk</to><from>HOD</from><heading>Issue Hall Tickets</heading><body>Issue the hall tickets for those who had required attendance</body></note>

K.Sasidhar

DTD ex: explanation

The DTD above is interpreted like this:

!DOCTYPE note defines that the root element of this document is note

!ELEMENT note defines that the note element contains four elements: "to,from,heading,body"

!ELEMENT to defines the to element to be of type "#PCDATA"

!ELEMENT from defines the from element to be of type "#PCDATA"

!ELEMENT heading defines the heading element to be of type "#PCDATA"

!ELEMENT body defines the body element to be of type "#PCDATA"

K.Sasidhar

External DTD declaration ex:

<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>

K.Sasidhar

DTD continue….

<?xml version="1.0"?>

<!DOCTYPE note SYSTEM "note.dtd"><note> <to>clerk</to> <from>HOD</from> <heading>Issue Hall Tickets </heading> <body> Issue the hall tickets for those who had required attendance </body></note>

K.Sasidhar

XML Building Blocks

Tags Elements Attributes Entities PCDATA (Parsable Character Data)

CDATA

K.Sasidhar

Element descriptions Suffixes:

? optional + one or more * zero or more

Separators , both, in order | or

Grouping ( ) grouping

K.Sasidhar

Empty elements

<!ELEMENT element-name EMPTY>

Example:

<!ELEMENT br EMPTY>

XML example:

<br />

K.Sasidhar

Elements with Parsed Character Data

<!ELEMENT from (#PCDATA)>

K.Sasidhar

Elements with any Contents

Elements declared with the category keyword ANY, can contain any

combination of parsable data <!ELEMENT element-name ANY>

Example:

<!ELEMENT note ANY>

K.Sasidhar

Elements with Children (sequences)

<!ELEMENT element-name (child1)>or<!ELEMENT element-name (child1,child2,...)>

Example:

<!ELEMENT note (to,from,heading,body)>

K.Sasidhar

DTD - Attributes In a DTD, attributes are declared with an

ATTLIST declaration. Syntax: <!ATTLIST element-name attribute-name

attribute-type default-value>

DTD example:

<!ATTLIST payment type CDATA "check">

XML example:

<payment type="check" />

K.Sasidhar

Attribute types The attribute-type can be one of the following:CDATA The value is character

data

(en1|en2|..) Value from enum list

ID Value is unique id

IDREF Value is id of another element

IDREFS Value is list of other ids

NMTOKEN Valid xml name

ENTITY Value is an entity

ENTITITES List of entities

notation Value is name of notation

Xml: Value is predefined xml value

K.Sasidhar

Elements vs. Attributes

Data can be stored in child elements or in attributes.

<person gender ="male"> <firstname>sravan</firstname> <lastname>kota</lastname></person>

gender is attribute in this example.

K.Sasidhar

Elements vs. Attributes continue.. <person>

<gender>male</gender> <firstname>sravan</firstname> <lastname>kota</lastname></person>

gender is child element in this example.

No rules are there when to use attributes or elements.

It depends on programmer If the programmer feels information is

useful better to use elements.

K.Sasidhar

Problems with attributes

attributes cannot contain multiple values (child elements can)

attributes are not easily expandable (for future changes)

attributes cannot describe structures (child elements can)

attributes are more difficult to manipulate by program code

attribute values are not easy to test against a DTD

K.Sasidhar

ENTITIES Entities are variables used to define shortcuts

to standard text or special characters. Entities can be declared internal or external

Internal entity syntax: <!ENTITY entity-name "entity-value">

DTD Example:<!ENTITY writer "Donald Duck."><!ENTITY copyright "Copyright of Disney.">

XML example:<author>&writer; &copyright;</author>

Note: entity has 3 parts. &, name and ;

K.Sasidhar

External Entity Declaration

Syntax <!ENTITY entity-name SYSTEM "URL">

DTD Example:

<!ENTITY college SYSTEM "http://www.sreenidhi.com/entities.dtd"><!ENTITY copyright SYSTEM "http://www.sreenidhi.com/entities.dtd">

XML example:<author>&writer;&copyright;</author>

Note: entity has 3 parts. &, name and ;

K.Sasidhar

Important Keywords in DTD

#REQUIRED

#IMPLIED

#FIXED

#PCDATA

K.Sasidhar

DTDs DISADVANTAGES DTDs are written in a syntax unrelated to

XML. Cannot be analyzed with an xml

processor Confusion to deal with two different

syntactic formats ( one to define document and two to define its structure).

DTD data types are not numeric. With DTDs the content of an element

specified as text even though it could be an integer, floating-point number or a range of numbers.

K.Sasidhar

XML Schema

XML Schema describes the structure of an XML document.

The XML Schema language is also referred to as XML Schema Definition

(XSD).

K.Sasidhar

XML Schema Vs DTDXml schema DTD

Created by using xml syntax.Xml style sheets (XSL) are used with xml schemas.

Not allowed with xsl

Supports Namespaces No namespaces

Supports more data types including number and derived data types

Supports only character data types

Easy to create and edit complex content

Difficult to reusable

Xml schema can be parsable by xml parsers

Cannot parsable

K.Sasidhar

Use of XML Schema in web design

Defines root and child elements that can appear in a document

Defines attributes that can appear in a document

Defines the order and 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

K.Sasidhar

XML Schema importance …..

XML Schemas are extensible to future additions

XML Schemas are richer and more powerful than DTDs

XML Schemas support data types and namespaces

K.Sasidhar

Importance continues…

XML Schema is an xml document so it can be parsed with an xml parser.

Provides more control over data types than DTDs.

XML schemas are namespace centric.

K.Sasidhar

Fundamentals

Schemas are like class and object in oop language.

Schema is similar to class definition

Xml document that conforms to the structure defined in the schema is similar to an object of the schema’s class.

K.Sasidhar

Purposes of schemas Have 2 primary purposes. First, a schema specifies the structure

of its instance XML documents, including which elements and attributes may appear in the instance document, as well as where and how often they may appear.

Second, schema specifies the data type of every element and attribute of its instance XML documents.

K.Sasidhar

Defining a schema

Every schema has schema as its root element.

Schema element specifies the namespace for the schema of schemas from which the schema’s elements and attributes will be drawn.

Xmlns:xsd = “http://www.w3.org/2001/XMLSchema”

K.Sasidhar

Ex Program

<?xml version="1.0"?><xs:schema

xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.sreenidhi.com"xmlns="http://www.sreenidhi.com"elementFormDefault="qualified">......</xs:schema>

K.Sasidhar

explanation

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:

targetNamespace="http://www.sreenidhi.com"

indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.sreenidhi.com" namespace.

K.Sasidhar

explanation

xmlns=http://www.sreenidhi.com indicates that the default

namespace is "http://www.sreenidhi.com".

elementFormDefault="qualified" indicates that any elements used

by the XML instance document which were declared in this schema must be namespace qualified.

K.Sasidhar

XML Schema reference

<?xml version="1.0"?>

<note xmlns="http://www.sreenidhi.com"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.sreenidhi.com note.xsd">

<to>clerk</to><from>HOD</from><heading>Issue Hall Tickets</heading><body> Issue the hall tickets for those who had required attendance </body></note>

K.Sasidhar

Data Types

Binary: Supports hexa code

Logic data types: Boolean

Number data types: Float, double, decimal,

long

Date and time data types: Time

(hh:mm:ss), duration, date, date time etc..

Text Data types: URL, string

XML Data types: Qualified names (qname)

K.Sasidhar

Facets

EX: Facets of integer primitive data type has eight possible facets:

totalDigits, maxInclusive, maxExclusive, minInclusive, minExclusive, pattern, enumeration and whitespace.

This list can be found at www.w3.org/TR/xmlschema-2/#built-

in-datatypes.

K.Sasidhar

Simple types

<xsd: element name=“engine” type = “xsd:string” />

An instance of schema in which the engine element is defined could have the following element

<engine> inline six cylinder fuel injected </engine>

K.Sasidhar

Ex:

<xsd: simpletype name= “firstname”>

<xsd: restriction base = “xsd:string”>

<xsd:maxLength value = “10”/> </xsd:restriction> </xsd:simpletype>

K.Sasidhar

Complex types

<xsd: complexType name = “sports_car”> <xsd:sequence> <xsd: element name = “make”

type=“xsd:string” /> <xsd: element name = “model”

type=“xsd:string” /> <xsd: element name = “engine”

type=“xsd:string” /> <xsd: element name = “year”

type=“xsd:decimal” /> </xsd:sequence> </xsd: complexType>