Understanding XML
description
Transcript of Understanding XML
![Page 1: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/1.jpg)
Understanding XML
An Introduction to XML
Sandeep Bhattaram
![Page 2: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/3.jpg)
Types of ‘Data’ *
Structured Data
Semi-Structured Data
Unstructured Data
![Page 4: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/10.jpg)
XML Syntax Rules
XML Data Model has TWO structuring concepts
1. Elements
2. Attributes
![Page 11: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/12.jpg)
XML Syntax Rules Contd... All XML documents must have a
root element – Tree Model <root> <child> <subchild>.....</subchild> </child> </root>
![Page 13: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/14.jpg)
XML Elements *
Elements classified w.r.t Contents1. element content2. mixed content3. simple content4. empty content
![Page 15: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/25.jpg)
XML DTD Contd... Building blocks of XML DTD 1. Elements2. Tags3. Attributes4. Entities - <, >, &, ",
'5. PCDATA – Parsed Character DATA6. CDATA – Character Data
![Page 26: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/28.jpg)
XML DTD Contd...
![Page 29: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/32.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/33.jpg)
XML SCHEMA- Example XML file and DTD
![Page 34: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/34.jpg)
XML SCHEMA - Example Schema
![Page 35: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/35.jpg)
XML Schema – Reference to DTD, XML schema
![Page 36: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/36.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/37.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/39.jpg)
XML Schema – Single value Facet, Facet on set of values
![Page 40: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/40.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/41.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/42.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/43.jpg)
XML Schema – Facets for Datatypes
![Page 44: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/44.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/45.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/46.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/47.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/48.jpg)
XML Documents & Databases
![Page 49: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/49.jpg)
XML Documents & Databases
![Page 50: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/50.jpg)
XML Documents & Databases
![Page 51: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/51.jpg)
XML Documents & Databases
![Page 52: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/52.jpg)
XML Documents & Databases
![Page 53: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/53.jpg)
XML Documents & Databases
![Page 54: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/54.jpg)
XML Documents & Databases
![Page 55: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/55.jpg)
Review of Topics Covered Introduction Types of Data XML Basics XML DTD XML Schema XML and Databases
![Page 56: Understanding XML](https://reader036.fdocuments.us/reader036/viewer/2022062422/56813bd4550346895da4f93b/html5/thumbnails/56.jpg)
References Database Management Systems,
Chapter 26 www.w3.org www.w3schools.com