understanding xml an introduction to xml sandeep bhattaram

56
Understanding XML An Introduction to XML Sandeep Bhattaram

Upload: maud-holmes

Post on 05-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Understanding XML An Introduction to XML Sandeep Bhattaram

Understanding XML

An Introduction to XML

Sandeep Bhattaram

Page 2: Understanding XML An Introduction to XML Sandeep Bhattaram

Summary of Introduction *

HTML was designed to ‘Display’ and format data based on ‘Syntax’

XML is designed to ‘Describe’ and structure data based on ‘Semantics’

Page 3: Understanding XML An Introduction to XML Sandeep Bhattaram

Types of ‘Data’ *

Structured Data

Semi-Structured Data

Unstructured Data

Page 4: Understanding XML An Introduction to XML Sandeep Bhattaram

Semi-Structured Data - An Example

Schema information is mixed with data objects and values

No predefined schema for the data to conform to.

Page 5: Understanding XML An Introduction to XML Sandeep Bhattaram

Unstructured Data No structure for the data to conform to. Example : HTML <table> <TR> <TD> XML Class </TD> <TD> very interesting</TD> </TR> </table>

Page 6: Understanding XML An Introduction to XML Sandeep Bhattaram

Basics of XML *

XML stands for eXtensible Markup Language

XML is a mark up language User defines his own tags in XML XML is Self-Descriptive

Page 7: Understanding XML An Introduction to XML Sandeep Bhattaram

Basic example of XML<note>

<to>Everyone</to> <from>Sandeep</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body>

</note>

XML doesn’t “DO” anything With XML your data is stored

outside your HTML

Page 8: Understanding XML An Introduction to XML Sandeep Bhattaram

Basics Contd.. Exchange of data between incompatible

systems via XML XML can be used to Store Data

Definition: XML is a cross-platform, software and

hardware independent tool for describing and transmitting information.

Page 9: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Syntax * The syntax rules of XML are very simple,

self-describing & very strict.

<?xml version="1.0" encoding="ISO-8859-1"?> <note>

<to>Everyone</to> <from>Sandeep</from>

<heading>Reminder</heading> <body>Don't forget me this

weekend!</body> </note>

Page 10: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Syntax Rules

XML Data Model has TWO structuring concepts

1. Elements

2. Attributes

Page 11: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Syntax Rules Contd...

All XML elements must have a closing tag

XML tags are case sensitive All XML elements must be properly

nested…NOT -> <B> <I> abc </B> </I>

Page 12: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Syntax Rules Contd... All XML documents must have a

root element – Tree Model <root> <child> <subchild>.....</subchild> </child> </root>

Page 13: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Syntax Rules Contd... Attribute values must always be

quoted <?xml version="1.0" encoding="ISO-8859-1"?> <note date=12/11/2002> <to>Everyone</to> <from>Sandeep</from> </note> Comments in XML <!-- This is a comment -->

Page 14: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Elements *

Elements classified w.r.t Contents1. element content2. mixed content3. simple content4. empty content

Page 15: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Elements Contd... Element – content example<book> <title>My First XML</title> <prod id="33-657” media="paper"></prod><chapter>Introduction to XML <para>What is HTML</para> <para>What is XML</para> </chapter> </book>

Page 16: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Elements Contd... XML Elements are Extensible<note> <to>Everyone</to> <from>Sandeep</from> <body>Don't forget me this weekend!</body> </note>

<date>2004-04-08</date>. Will the application crash ??

Page 17: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Element Naming Rules

XML elements must follow these basic naming rules:

Names can contain letters, numbers, and other characters

Names must not start with a number or punctuation character

Names must not start with the letters xml (or XML or Xml ..)

Names cannot contain spaces

Page 18: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Attributes * <img src="computer.gif"> - HTML <person sex="female"> - XML So is this right?? <note day="12" month="11"

year="2002" to=“Everyone" from=“Sandeep” heading="Reminder" body="Don't forget me this weekend!"> </note>

Page 19: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Attributes Contd... NO ! Use Child Elements:1. <date>12/11/2002</date> 2. <date> <day>12</day> <month>11</month>

<year>2002</year> </date>

Not <date day = “11” month = “12”...></date>

Page 20: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Attributes Contd...Problems using attributes attributes cannot contain multiple values attributes are not easily expandable attributes cannot describe structures attributes are more difficult to manipulate by

program code attribute values are not easy to test against a

Document Type Definition (DTD) - which is used to define the legal elements of an XML document

When do we use attributes ?

Page 21: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Documents * Basic object in XML Types:1. Data-Centric2. Document-Centric3. Hybrid XML Documents Document Declaration <?xml version="1.0" encoding="ISO-

8859-1” standalone = “yes”?>

Page 22: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Document Type Definition

Well Formed XML Document = syntax Valid XML Document = Well Formed XML Document + Conforms to DTD/XSD rules.

DefinitionA DTD defines the legal elements of an XML

document.

Page 23: Understanding XML An Introduction to XML Sandeep Bhattaram

XML DTD Contd... Inline DTD Document <!DOCTYPE root-element [element-declarations]> <?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>Everyone</to> <from>Sandeep</from>

<heading>Reminder</heading> <body>Don't forget me this weekend</body> </note>

Page 24: Understanding XML An Introduction to XML Sandeep Bhattaram

XML DTD Contd... External DTD <!DOCTYPE root-element SYSTEM "filename">

<!DOCTYPE note SYSTEM "note.dtd"> <note>... </note> <!DOCTYPE note SYSTEM

"http://www.uark.edu/dtd/note.dtd"> <note> … </note>

Page 25: Understanding XML An Introduction to XML Sandeep Bhattaram

XML DTD Contd... Building blocks of XML DTD 1. Elements2. Tags3. Attributes4. Entities - &lt;, &gt;, &amp;, &quot;,

&apos;5. PCDATA – Parsed Character DATA6. CDATA – Character Data

Page 26: Understanding XML An Introduction to XML Sandeep Bhattaram

XML DTD Contd... DTD Element Declarations (w.r.t content)1. Empty - <!ELEMENT element-name EMPTY>

2. Character - <!ELEMENT element-name (#PCDATA)>

3. Any - <!ELEMENT element-name ANY>

4. Children – <!ELEMENT element-name

(child-element-name,child-element-name,.....)> + (required multivalued), * optional

multivalued, | (or), ? (optional singlevalued), required single valued

Page 27: Understanding XML An Introduction to XML Sandeep Bhattaram

XML DTD Contd... XML DTD Attribute Declaration <!ATTLIST element-name attribute-name attribute-type default-value>

Attribute Types

Default Types: value, EMPTY, #REQUIRED, #IMPLIED, #FIXED “value”

Page 28: Understanding XML An Introduction to XML Sandeep Bhattaram

XML DTD Contd...

Page 29: Understanding XML An Introduction to XML Sandeep Bhattaram

XML DTD Contd...

Limitations of DTD:

1. Data types in DTD are not very general

2. DTD needs specialized processors3. Unordered elements are not

permitted

Page 30: Understanding XML An Introduction to XML Sandeep Bhattaram

XML SCHEMA * XML Schema is used to structure the

XML Document into ‘legal’ blocks.

Advantages:1. Supports data types2. Written in XML3. Facilitates secure data

communication.

Page 31: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Key Points

XML Schema defines Elements Attributes What are the data types of elements and

attributes Number of Children, Copies for an element Which elements are children, or have text or

are empty Order of Children

Page 32: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema-Key Points contd

XML Schema supports Name Spacesdf/f:note, df/f:note xmlns:df/f = “www…..” Data Types Extensible to future additions

XML Schema is a W3C Recommendation now!

Page 33: Understanding XML An Introduction to XML Sandeep Bhattaram

XML SCHEMA- Example XML file and DTD

Page 34: Understanding XML An Introduction to XML Sandeep Bhattaram

XML SCHEMA - Example Schema

Page 35: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Reference to DTD, XML schema

Page 36: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema - Simple Elements *

A simple element is an XML element that can contain only text

<xs:element name="xxx" type="yyy"/> Example - <lastname>Refsnes</lastname> <xs:element name="lastname"

type="xs:string"/> XML Schema Datatypes: xs:string,xs:decimal,xs:integer,xs:boolean,xs:date,xs:time

<xs:element …… default = “aaa” /> <xs: element …… fixed = “bbb” />

Page 37: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Attributes * All attributes are declared as simple types. Only complex elements can have attributes! <xs:attribute name="xxx" type="yyy"/> Example -<lastname

lang="EN">Smith</lastname> <xs:attribute name="lang" type="xs:string"/> fixed = “ ”, default = “ ” use = “optional/required”

Page 38: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Facets * Facets are restrictions applied on Elements

and Attributes

Facets , Constraint used– 1. Single value, minInclusive maxInclusive etc2. Series of values, enumeration3. White spaces, whiteSpace4. Length, length minLength maxLength etc

Restrictions on Datatypes

Page 39: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Single value Facet, Facet on set of values

Page 40: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Facets on Series of values

Pattern constraint – [],[][]..,([][]..)*, ([][]..)+, ([]|[]|..), [] {}

Example: <xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> ///or [a-zA-

Z]</xs:restriction>

</xs:simpleType> </xs:element>

Page 41: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Facets on White space characters

<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string">

<xs:whiteSpace value="preserve"/> </xs:restriction>

</xs:simpleType> </xs:element> <xs:whiteSpace value="replace"/> <xs:whiteSpace value="collapse"/>

Page 42: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Facets on Length

Constraints = length, minLength, maxLength

Example: <xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpleType> </xs:element>

Page 43: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Facets for Datatypes

Page 44: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema –Complex Elements*

A complex element is an XML element that contains other elements and/or attributes.

There are four kinds of complex elements:1. empty elements 2. elements that contain only other elements 3. elements that contain only text 4. elements that contain both other elements

and text

Page 45: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Complex Elements example – Elements only

EXAMPLE: <employee> <firstname> .. </firstname> <lastname>..</lastname> </employee> <xs:element name="employee" 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>

Page 46: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Complex Elements example – Empty elements only

EXAMPLE: <product prodid="1345" /> <xs:element name="product” type="prodtype"/>

<xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/>

</xs:complexType> </xs:element>

Similarly for Text Only elements and Mixed elements

Page 47: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Schema – Indicators * Indicators are used to control How these

elements are used in the documents. Order Indicators: All, Choice, Sequence Occurrence Indicators: maxOccurs, minOccurs Group Indicators: Group name, attributeGroup name See Text Book Example.

Page 48: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Documents & Databases

Page 49: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Documents & Databases

Page 50: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Documents & Databases

Page 51: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Documents & Databases

Page 52: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Documents & Databases

Page 53: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Documents & Databases

Page 54: Understanding XML An Introduction to XML Sandeep Bhattaram

XML Documents & Databases

Page 55: Understanding XML An Introduction to XML Sandeep Bhattaram

Review of Topics Covered Introduction Types of Data XML Basics XML DTD XML Schema XML and Databases

Page 56: Understanding XML An Introduction to XML Sandeep Bhattaram

References Database Management Systems,

Chapter 26 www.w3.org www.w3schools.com