IS446: Internet Software Development
Dr. Azeddine Chikh
2
Part 2. XML Basics
I. Introduction
II. XML
I. Introduction
Introduction (1)
5
SGML (1960)
XML(1996)
HTML(1990) XHTML(2000)
XML (1) XML was designed to transport and store data. HTML was designed to display data.
6
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
The difference Between XML and HTMLXML is not a replacement for HTML.XML and HTML were designed with different goals.HTML is about displaying information, while XML is about carrying information.
XML (3)
7
XML is Just Plain TextXML is nothing special. It is just plain text. Software that can handle plain text can also handle XML. However, XML-aware applications can handle the XML tags specially. The functional meaning of the tags depends on the nature of the application.
With XML You Invent Your Own TagsThe tags in the example above (like <to> and <from>) are not defined in any XML standard. These tags are "invented" by the author of the XML document.That is because the XML language has no predefined tags.The tags used in HTML (and the structure of HTML) are predefined. HTML documents can only use tags defined in the HTML standard (like <p>, <h1>, etc.).XML allows the author to define his own tags and his own document structure.
XML (4)
8
XML is Not a Replacement for HTMLXML is a complement to HTML.It is important to understand that XML is not a replacement for HTML. In most web applications, XML is used to transport data, while HTML is used to format and display the data.The best description of XML is this:XML is a software and hardware independent tool for carrying information.
XML is a W3C RecommendationXML became a W3C Recommendation 10. February 1998.
XML is EverywhereXML is everywhere. It is the most common tool for data transmissions between all sorts of applications, and is becoming more and more popular in the area of storing and describing information.
How to use (1)
9
XML Separates Data from HTMLIf you need to display dynamic data in your HTML document, it will take a lot of work to edit the HTML each time the data changes.With XML, data can be stored in separate XML files. This way you can concentrate on using HTML for layout and display, and be sure that changes in the underlying data will not require any changes to the HTML.
XML Simplifies Data SharingIn the real world, computer systems and databases contain data in incompatible formats.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 different applications can share.
How to use (2)
10
XML Simplifies Data TransportWith XML, data can easily be exchanged between incompatible systems.One of the most time-consuming challenges for developers is to exchange data between incompatible systems over the Internet.Exchanging data as XML greatly reduces this complexity, since the data can be read by different incompatible applications.
XML Simplifies Platform ChangesUpgrading to new systems (hardware or software platforms), is always very time consuming. Large amounts of data must be converted and incompatible data is often lost.XML data is stored in text format. This makes it easier to expand or upgrade to new operating systems, new applications, or new browsers, without losing data.
How to use (3)
11
XML Makes Your Data More AvailableSince XML is independent of hardware, software and application, XML can make your data more available and useful.With XML, your data can be available to all kinds of "reading machines" (Handheld computers, voice machines, news feeds, etc), and make it more available for blind people, or people with other disabilities.
XML is Used to Create New Internet LanguagesA lot of new Internet languages are created with XML. Here are some examples:XHTML the latest version of HTML WSDL for describing available web services WAP and WML as markup languages for handheld devices RDF and OWL for describing resources and ontology SMIL for describing multimedia for the web
If Developers Have SenseIf they DO have sense, future applications will exchange their data in XML.The future might give us word processors, spreadsheet applications and databases that can read each other's data in a pure text format, without any conversion utilities in between.
II. XML
Documents (1)
XML is expressed as “documents”, whether an entire book or a database record
Must have: At least one element Only one “root” element
Should have: A document type declaration; e.g.,
<?xml version="1.0"?> Namespace declarations
Can have: One or more properly nested elements (XML Tree) Comments Processing instructions
14
<?xml version:”1.0”?>
<?xml-stylesheet type="text/xsl" href=“template.xsl"?>
<!-- File name: Bibliography.xml -->
<Bibliography> <Book ISBN=“1-111-122”>
<Title> Java </Title>
<Author> Mustafa </Author>
<Year> 1995 </Year> </Book>
.
. <Book>
<Title> Oracle </Title>
<Author> Emad </Author><Year> 1973 </Year>
</Book></Bibliography>
Comments
Root or documentelement
Elements nestedWithin root element
Attribute
Processinginstruction
XMLDeclaration
The Anatomy of XML Document
Documents (2)
15
The XML declaration looks like this: <?xml version="1.0" encoding="UTF-8“ standalone="yes"?
>
The XML declaration is not required by browsers, but is required by most XML processors (so include it!)
If present, the XML declaration must be first--not even white space should precede it
Note that the brackets are <? and ?>
version="1.0" is required (I am not sure it is the only version so far)
encoding can be "UTF-8" (ASCII) or "UTF-16" (Unicode), or something else, or it can be omitted
standalone tells whether there is a separate DTD
Documents (3)
XML Tree (1)
16
An Example XML Document
XML documents use a self-describing and simple syntax:
<?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>
• The terms parent, child, and sibling are used to describe the relationships between elements. • Parent elements have children. • Children on the same level are called siblings (brothers or sisters).• All elements can have text content and attributes (just like in HTML).
XML Tree (2)
17
<bookstore> <book category="COOKING">
<title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price>
</book> <book category="CHILDREN">
<title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price>
</book> <book category="WEB">
<title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price>
</book> </bookstore>
XML Elements (1)
18
What is an XML Element?
An XML element is everything from (including) the element's start tag to (including) the element's end tag.
An element can contain other elements, simple text or a mixture of both. Elements can also have attributes.
XML Elements (2)
19
XML Naming Rules
XML elements must follow these 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, etc) •Names cannot contain spaces •Any name can be used, no words are reserved.
XML Attributes (1)
20
XML Attributes
From HTML you will remember this: <img src="computer.gif">. The "src" attribute provides additional information about the <img> element.In HTML (and in XML) attributes provide additional information about elements:
<img src="computer.gif"> <a href="demo.asp">Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:
<file type="gif">computer.gif</file>
XML Attributes (2)
21
XML Attributes Must be Quoted
Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person's citizenship, the person tag can be written like this:
<person citizenship="german"> or like this:
<person citizenship=‘german'>
If the attribute value itself contains double quotes you can use single quotes, like in this example:
<gangster name='George "Shotgun" Ziegler'> or you can use character entities:
<gangster name="George "Shotgun" Ziegler">
XML Attributes (3)
22
XML Elements vs. Attributes
<person citizenship=“american"> <firstname>Anna</firstname> <lastname>Smith</lastname>
</person>
<person> <citizenship>american</citizenship> <firstname>Anna</firstname> <lastname>Smith</lastname>
</person>
Both examples provide the same information. There are no rules about when to use attributes and when to use elements. In XML it is recommended to avoid them and to use elements instead.
23
XML Attributes (4)
Should you avoid using attributes?
Here are some of the problems using 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 Document Type Definition (DTD) - which is used to define the legal elements of an XML document
Comments
You can embed comments in your XML just like in HTML:
<!-- Whatever is here (whether text or markup) will be ignored on processing -->
25
Processing Instructions
PIs (Processing Instructions) may occur anywhere in the XML document (but usually in the beginning)
A PI is a command to the program processing the XML document to handle it in a certain way
XML documents are typically processed by more than one program
Programs that do not recognize a given PI should just ignore it
General format of a PI: <?target instructions?>
Example: <?xml-stylesheet type="text/css“ href="mySheet.css"?>
26
More XML components
Namespace
Entities
CDATA
27
Namespace
Overview
Declaration
Default namespace
Scope
attribute
28
Namespaces: Overview
Part of XML’s extensibility
Allow authors to differentiate between tags of the same name (using a prefix)
Frees author to focus on the data and decide how to best describe it
Allows multiple XML documents from multiple authors to be merged
Identified by a URI (Uniform Resource Identifier)
When a URL is used, it does NOT have to represent a live server
29
Namespaces: Declaration
xmlns: bk = “http://www.example.com/bookinfo/”
URI(URL)Prefix
Namespace declaration
Example:
<BOOK xmlns:bk=“http://www.bookstuff.org/bookinfo”>
<bk:TITLE> All About XML </bk:TITLE><bk:AUTHOR> Joe Developer </bk:AUTHOR><bk:PRICE currency=‘US Dollar’> 19.99
</bk:PRICE></BOOK>
30
Namespaces: Default Namespace
An XML namespace declared without a prefix becomes the default namespace for all sub-elements
All elements without a prefix will belong to the default namespace:
<BOOK xmlns=“http://www.bookstuff.org/bookinfo”><TITLE> All About XML </TITLE><AUTHOR> Joe Developer </AUTHOR>
</BOOK>
31
Namespaces: Scope
Unqualified elements belong to the inner-most default namespace.
BOOK, TITLE, and AUTHOR belong to the default book namespace
PUBLISHER and NAME belong to the default publisher namespace
<BOOK xmlns=“www.bookstuff.org/bookinfo”><TITLE> All About XML </TITLE><AUTHOR> Joe Developer </AUTHOR><PUBLISHER xmlns=“urn:publishers:publinfo”>
<NAME> Microsoft Press </NAME></PUBLISHER>
</BOOK>
32
Namespaces: Attributes
Unqualified attributes do NOT belong to any namespace
Even if there is a default namespace
This differs from elements, which belong to the default namespace
33
Entities
Entities provide a mechanism for textual substitution,
e.g.
You can define your own entities
Parsed entities can contain text and markup
Unparsed entities can contain any data JPEG photos, GIF files, movies, etc.
Entity Substitution
< <
& &
34
CDATA
By default, all text inside an XML document is parsed
You can force text to be treated as unparsed character data by enclosing it in <![CDATA[ ... ]]>
Any characters, even & and <, can occur inside a CDATA
White space inside a CDATA is (usually) preserved
The only real restriction is that the character sequence ]]> cannot occur inside a CDATA
CDATA is useful when your text has a lot of illegal characters (for example, if your XML document contains some HTML text)
<message> <![CDATA[<greeting>Hello, world!</greeting>]]> </message>
XML Validation (1)
35
• XML with correct syntax is "Well Formed" XML
• XML validated against a DTD is "Valid" XML.
Well Formed XML Documents
A "Well Formed" XML document has correct XML syntax
• XML documents must have a root element • XML elements must have a closing tag • XML tags are case sensitive • XML elements must be properly nested • XML attribute values must be quoted
<?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>
XML Validation (2)
36
Valid XML Documents
A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD):
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE note SYSTEM "Note.dtd"><note>
<to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body>
</note>
The DOCTYPE declaration in the example above, is a reference to an external DTD file. The content of the file is shown in the next slide.
XML Validation (3)
37
XML DTD
The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements:
<!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]>
XML Validation (4)
38
XML Schema
W3C supports an XML based alternative to DTD called XML Schema:
<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> </xs:complexType>
</xs:element>
Viewing XML files (1)
39
Viewing XML Files
<?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>
Examples: 1.note.xml2.This is a breakfast food menu from a restaurant, stored as XML data.
Viewing XML files (2)
40
Displaying XML with CSS
1. The XML file: The CD catalog
2. The style sheet: The CSS file
3. The view: The CD catalog formatted with the CSS file
Viewing XML files (3)
41
Displaying XML with CSS
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/css" href="cd_catalog.css"?><CATALOG>
<CD> <TITLE>Empire Burlesque</TITLE><ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR>
</CD> <CD>
<TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR>
</CD> . . . . </CATALOG>
Viewing XML files (4)
42
Displaying XML with XSL
1. The XML file: food menu
2. The style sheet: The XSL file
3. The view: The food menu formatted with the XSL file
Viewing XML files (5)
43
Displaying XML with XSL
<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="simple.xsl"?> <breakfast_menu>
<food> <name>Belgian Waffles</name><price>$5.95</price> <description> two of our famous Belgian Waffles </description> <calories>650</calories>
</food> </breakfast_menu>
44
References W3 Schools XML Tutorial
http://www.w3schools.com/xml/default.asp
W3C XML page
http://www.w3.org/XML/
XML Tutorials
http://www.programmingtutorials.com/xml.aspx
Online resource for markup language technologies
http://xml.coverpages.org/
Several Online Presentations
45
END