EXTENSIBLE MARKUP LANGUAGE (XML) 1
1
EXTENSIBLE
MARKUP
LANGUAGE
(XML)
2
WHAT IS XML?
eXtensible Markup Language markup language for documents containing
structured information much like HTML XML was designed to carry data, not to display
data XML tags are not predefined. You must define
your own tags Based on Standard Generalized Markup
Language (SGML) Version 1.0 introduced by World Wide Web
Consortium (W3C) in 1998 Bridge for data exchange on the Web
3
WHAT IS XML? a method for putting structured data into a text
file; these files are:Easy to readUnambiguousExtensiblesoftware- and hardware-independent tool for
carrying information.self-descriptive
4
WHAT IS XML?
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.
5
HOW CAN XML BE USED?
XML Separates Data from HTML XML Simplifies Data Sharing XML Simplifies Data Transport XML Simplifies Platform Changes XML Makes Your Data More Available XML is Used to Create New Internet Languages
XHTML WSDL for describing available web services WAP and WML as markup languages for handheld
devices RSS languages for news feeds
6
If Developers Have Sense
If they DO have sense, future applications will exchange their data in XML.
7
COMPARISONS
Extensible set of tags Content orientated Allows multiple output
forms
Fixed set of tags Presentation oriented Single presentation
XML HTML
8
AUTHORING XML ELEMENTS An XML element is made up of a start tag, an
end tag, and data in between. Example: <director> Matthew Dunn </director> Example of another element with the same
value: <actor> Matthew Dunn </actor> XML tags are case-sensitive: <CITY> <City> <city> XML can abbreviate empty elements, for
example: <married> </married> can be abbreviated to <married/>
9
AUTHORING XML DOCUMENTS
A basic XML document is an XML element that can, but might not, include nested XML elements.
Example: <books> <book isbn=“123”> <title> Second Chance </title> <author> Matthew Dunn
</author> </book> </books>
10
XML DATA MODEL: EXAMPLE
<BOOKS><book id=“123”
loc=“library”> <author>Hull</author> <title>California</title> <year> 1995 </year></book><article id=“555”
ref=“123”> <author>Su</author> <title> Purdue</title></article></BOOKS> Hull Purdue
BOOKS
123 555
California
Su
titleauthor
title
author
articlebook
year
1995
ref
loc=“library”
11
XML 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>
12
XML SYNTAX RULES All XML Elements Must Have a Closing
TagIn HTML, some elements do not have to have a closing tag: <p>This is a paragraph
<p>This is another paragraph In XML, it is illegal to omit the closing tag. All elements must have a closing tag:
<p>This is a paragraph</p><p>This is another paragraph</p>
13
XML SYNTAX RULES XML Tags are Case Sensitive
XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.
Opening and closing tags must be written with the same case:<Message>This is incorrect</message><message>This is correct</message>
14
XML SYNTAX RULES XML Elements Must be Properly Nested
In HTML, you might see improperly nested elements:<b><i>This text is bold and italic</b></i> In XML, all elements must be properly nested within each other:<b><i>This text is bold and italic</i></b>
15
XML SYNTAX RULES XML Documents Must Have a Root
ElementXML documents must contain one element that is the parent of all other elements. This element is called the root element.<root> <child> <subchild>.....</subchild> </child></root>
16
XML SYNTAX RULES XML Attribute Values Must be Quoted XML elements can have attributes in name/value
pairs just like in HTML. In XML, the attribute values must always be quoted. Incorrect:
<note date=12/11/2007> <to>Tove</to> <from>Jani</from></note>
Correct:<note date="12/11/2007"> <to>Tove</to> <from>Jani</from></note>
17
XML SYNTAX RULES : ENTITY REFERENCES
special meaning of Some characters in XML. Error in XML:
<message>if salary < 1000 then</message> To avoid this error, replace the "<" character with an
entity reference: <message>if salary < 1000 then</message>
There are 5 predefined entity references in XML: < < less than > > greater than & & ampersand ' ' apostrophe " " quotation mark
Note: Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it.
18
Comments in XML The syntax for writing comments in XML is similar
to that of HTML.<!-- This is a comment -->
XML Syntax Rules
19
White-space is Preserved in XML HTML truncates multiple white-space characters
to one single white-space: HTML: Hello Tove Output: Hello Tove
XML: the white-space in a document is not truncated.
Hello Tove Output: Hello Tove
XML Syntax Rules
20
XML Stores New Line as LF Windows applications : a new line -> carriage
return (CR) and line feed (LF). Unix applications : a new line -> LF character Macintosh applications : a new line -> LF
character XML stores a new line as LF.
XML Syntax Rules
21
A Well Formed
XML document
22
XML DECLARATION
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
whitespace should precede it Note that the brackets are <? and ?> version="1.0" is required (this 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
23
An element can contain: other elements text attributes or a mix of all of the above...
XML Elements
24
<bookstore> <book category="CHILDREN"> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book></bookstore>
XML Elements
25
XML elements must follow these naming rules: Names can contain letters, numbers, and other
characters Names cannot start with a number or punctuation
character Names cannot start with the letters xml (or XML, or
Xml, etc) Names cannot contain spaces
XML Elements Naming Rules
26
<person sex="female"> <firstname>Anna</firstname> <lastname>Smith</lastname></person> <person> <sex>female</sex> <firstname>Anna</firstname> <lastname>Smith</lastname></person>
XML Elements vs. Attributes
27
1) <note date="10/01/2008"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>
XML Elements vs. Attributes
28
2) <note> <date>10/01/2008</date> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>
XML Elements vs. Attributes
29
3) <note> <date> <day>10</day> <month>01</month> <year>2008</year> </date> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>
XML Elements vs. Attributes
30
Some of the problems with using attributes are: attributes cannot contain multiple values (elements
can) attributes cannot contain tree structures (elements
can) attributes are not easily expandable (for future
changes) Attributes are difficult to read and maintain. Use
elements for data. Use attributes for information that is not relevant to the data.
Avoid XML Attributes?
31
XML Validation
32
XML with correct syntax is "Well Formed" XML.
XML validated against a DTD or XMLSchema is "Valid" XML.
XML Validation
33
34