Professional Documents
Culture Documents
Lecture 2
HTML uses a fixed, unchangeable set of In XML, you make up your own tags
tags
XML and HTML cont..
Simple XML Document
How to write XML Document
Open a text editor, such as Notepad
Save your XML file as “~.xml”
Simple XML Document cont..
How to write XML Document
Open the XML using a web browser
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Notebook> <Notebook>
<Processor>Intel Core i7-2710QE</Processor> <Processor>Intel Core i7-2710QE</Processor>
<Memory>DDR3L 1600</Memory> <Memory>DDR3L 1600</Mem>
<HardDisk>SSHD 1TB</HardDisk> <HardDisk>SSHD 1TB</HardDisk>
</Notebook> </Notebook>
Syntax Rule:
• XML documents must have a root element
• XML elements must have start and end tags
• XML tags are case sensitive
• XML elements must be properly nested
• XML attributed must always be quoted
XML Example
<?xml version="1.0" encoding="UTF-8"?> XML declaration
<!-- bookstore.xml --> XML comment
<bookstore> Root element start-tag
<book ISBN="101223547"> (one and only one root)
<title>Data Structure</title> First child element start-tag
<author>Willian Wong</author> (with an attribute of ISBN)
<year>2016</year>
</book> First child element end-tag
<book ISBN="121329748"> Second child element start-tag
<title>Guide to Travelling</title>
<author>Steven Muthu</author>
<author>Mary Ong</author>
<author>Ali Ahmad</author>
<year>2013</year>
Second child element end-tag
</book>
Root element end-tag
</bookstore>
Logical View of XML
Root element
bookstore Solid line
Content
XML Declaration
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Content is optional.
Empty element is an element without content.
Example of empty element:
<product />
<product></product>
<product code="1345" />
Root Element
There is exactly ONE element, called the root, or
document element.
<?xml version=“1.0”>
<Book>
<Title>Java Programming</Title>
<Author>Jimmy Kong</Author>
</Book>
<?xml version=“1.0”>
<Title>Java Programming</Title> <?xml version=“1.0”>
<Author>Jimmy Kong</Author> This is unvalid xml document
Element Example
<article> Start Tag
<author>Gerhard Weikum</author>
<title>The Web in Ten Years</title>
<text>
<abstract>In order to evolve...</abstract>
<section number=“1” title=“Introduction”>
The <index>Web</index> provides the universal...
</section>
</text>
</article>
Content of the Element
(Subelements and/or Text)
End Tag Element
Attribute
XML elements can have zero and more attributes in the
start tag.
Attribute are specified on the start tag of an element
An attribute consists of name and value pair.
Attribute must be quoted.
Example of attributes:
<student id="1011">John</student>
Attribute Example
<article>
<author>Gerhard Weikum</author>
<title>The Web in Ten Years</title>
<text>
<abstract>In order to evolve...</abstract>
<section number=“1” title=“Introduction”>
The <index>Web</index> provides the universal...
</section>
</text>
</article>
Attributes with name and value
Naming Rules of Element and Attribute
Names can contain letters, numbers, and other characters.
Names must not start with a number or punctuation
character.
Names cannot contain spaces.
Names must not start with the letters xml or XML.
Colon should not be used in XML names except for
namespace purposes.
Comments
Comments begin with <!-- and end with -->.
Comments can be placed anywhere in a document outside
other markup.
Comments are not part of the textual content of an XML
document.
The string “--” (double-hyphen) MUST NOT occur within
comments.
Character Data
The text between the start and end tags is defined as
‘character data’.
Character data may be any legal (Unicode) except ( < or >
or ” or ’ or &)
Character data is classified into:
PCDATA
CDATA
XML Namespace
To provide uniquely named elements and attributes to avoid name
conflicts in an XML document
If each vocabulary is given a namespace, the ambiguity between
XML Namespace cont..
XML Namespace Declaration
Using ‘xmlns’ attribute
Applying Namespaces
Element or attribute name starts with the prefix and “:”
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</record>
Default Namespace
It is an unprefixed declaration of Namespace.
It applies to all unprefixed element names within its
scope.
xmlns="namespaceURI"
Example:
<?xml version="1.0" encoding="UTF-8"?>
<Notebook xmlns="http://www.notebook.com">
<Processor>Intel Core i7-2710QE</Processor>
<Memory>DDR3L 1600</Memory>
<HardDisk>SSHD 1TB</HardDisk>
</Notebook>
Well-formed XML
An XML document with correct syntax is called "Well
Formed".
Every XML document must have a root element
Every element must have both start tag and end tag
e.g. <name> ... </name>
Empty-element tag must be properly closed, e.g., <book />.
XML tags are case sensitive
Elements must be properly nested
e.g. <book><title>...</book></title> is incorrectly nested.
Attribute values must be enclosed in single or double quotes
e.g. <time unit="days">
Benefit of using XML
XML separates data from HTML
With XML, data can be stored in separate XML files. Thus, changes
in the underlying data will not require any changes to the HTML.
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
Exchange data between incompatible systems over the Internet is
troublesome and time-consuming.
Exchanging data as XML greatly reduces this complexity, since the
data can be read by different incompatible applications.
Benefit of using XML cont..
XML simplifies platform change
Upgrading to new systems (hardware or software platforms), is
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, or new
applications, without losing data.
XML increases data availability
With XML, data can be available to all kinds of "reading
machines" (Handheld computers, voice machines, etc), and
make it more available for blind people, or people with other
disabilities.
XML-related Format
XML-related Technologies
DTD (Document Type Definition) and XML Schemas are used
to define legal XML tags and their attributes for particular
purposes