Introduction to XML


 Standard Generalized Markup Language (SGML)


What is XML?


HTML Vs XML


More on XML…

<employee>
<lastname>Chan</lastname>
<firstname>Peter</ firstname >
<department>Business Administration </department>
<phone>22567123</phone>
</employee>

 


Markup language created using XML


XML Syntax

<?xml version="1.0"?>
<employee>
   <lastname>Chan</lastname>
   <firstname>Peter</firstname >
   <department>Business Administration </department>
   <phone>22567123</phone>
</employee>

<?xml version="1.0"?> : the XML declaration, it defines the XML version of the document.

<employee> …</employee> : describes the root element of the document. This document is about an employee

<p>This is a paragraph</p>
<p>This is another paragraph</p>

<root>
<child>
<subchild>.....</subchild>
</child>
</root>

Book Title: Computer Network
Chapter 1: Introduction to Networking
What is Networking?
History of Networking
Chapter 2: OSI model
What is OSI model?
Layers of OSI model

<book>
<title>Computer Network</title>
<prod id="1234-5678" media="paper"></prod>
<chapter>Introduction to Networking
<para> What is Networking?</para>
<para> History of Networking </para>
</chapter>
<chapter> OSI model
<para> What is OSI model?</para>
<para> Layers of OSI model </para>
</chapter>
</book>

Book is the root element, it is the parent element of both title and chapter.

Title and chapter are child elements of Book.

<employee appoint_date="1/9/94">
<lastname>Chan</lastname>
<firstname>Peter</firstname >
<department>Business Administration </department>
<phone>22567123</phone>
</employee>

<employee>
<appoint_date>1/9/94</appoint_date>
<lastname>Chan</lastname>
<firstname>Peter</ firstname >
<department>Business Administration </department>
<phone>22567123</phone>
</employee>

<employee>
<appoint_date>
   <day>1</day>
   <month>9</month>
    <year>94/year>
</appoint_date>
<lastname>Chan</lastname>
<firstname>Peter</firstname >
<department>Business Administration </department>
<phone>22567123</phone>
</employee>

<staff>
<employee ID="120">
<lastname>Chan</lastname>
<firstname>Peter</firstname>
<department>Business Administration </department>
<phone>22567123</phone>
</employee>
<employee ID="121">
<lastname>Au</lastname>
<firstname>Albert</firstname>
<department>Computing</department>
<phone>22567321</phone>
</employee>
</staff>

XML Validation


XML Document Type Definition (DTD)


XML document with Internal DTD

<?xml version="1.0"?>
<!DOCTYPE employee [
<!ELEMENT employee (lastname, firstname, department, phone?)>
<!ATTLIST employee sex (male | female) "female">
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT department (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>


<!-- Data -->
<employee  sex ="male">
<lastname>Chan</lastname>
<firstname>Peter</firstname>
<department>Business Administration </department>
<phone>22567123</phone>
</employee>

Note:

(1) !ELEMENT employee (in line 3) defines the element "employee" as having four elements: 
" lastname, firstname, department, phone".

(2) !ATTLIST employee (in line 4) defines the attribute "sex"

(3) !ELEMENT lastname (in line 5) defines the "lastname" element as the type "PCDATA". PCDATA is parsed character data

(4) phone? (in line 3) indicates that element "phone" is optional. 
      (phone* indicates 0 or more occurrences
      phone+ indicates 1 or more occurrences)


XML document with External DTD

<?xml version="1.0"?> 

<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<lastname>Chan</lastname>
<firstname>Peter</firstname>
<department>Business Administration </department>
<phone>22567123</phone>
</employee>

File "employee.dtd" containing the Document Type Definition:

<?xml version="1.0"?>
<!ELEMENT employee (lastname, firstname, department, phone)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT department (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>


Displaying XML with JavaScript

<?xml version="1.0"?>
<employee>
<lastname>Chan</lastname>
<firstname>Peter</firstname>
<department>Business Administration </department>
<phone>22567123</phone>
</employee>

<html>
<head>
<script language="JavaScript" for="window" event="onload">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("employee.xml")
nodes = xmlDoc.documentElement.childNodes
lastname.innerText = nodes.item(0).text
firstname.innerText = nodes.item(1).text
department.innerText = nodes.item(2).text
phone.innerText = nodes.item(3).text
</script>
<title>HTML using XML data</title>
</head>
<body>
<h1>Employee data</h1>
<b>Lastname: </b><span id="lastname"></span><br>
<b>Firstname: </b><span id="firstname"></span> <hr>
<b>Department Name : </b><span id="department"></span><br>
<b>Phone number : </b><span id="phone"></span>
</body>
</html>

Employee data

Lastname: Chan

Firstname: Peter


Department Name : Business Administration

Phone number : 22567123

 


Displaying XML with CSS

<?xml version="1.0" ?>
<?xml-stylesheet type="text/css" href="employee.css"?>
<Stafflist>
<employee>
<lastname>Chan</lastname>
<firstname>Peter</firstname>
<department>Business Administration </department>
<phone>22567123</phone>
</employee>
<employee>
<lastname>Au</lastname>
<firstname>Albert</firstname>
<department>Computing</department>
<phone>22567321</phone>
</employee>
</Stafflist>

Stafflist
{
background-color: #ffffff;
width: 100%;
}
employee
{
display: block;
margin-bottom: 30pt;
margin-left: 0;
}
lastname
{
color: #FF0000;
font-size: 30pt;
}
firstname
{
color: #0000FF;
font-size: 20pt;
}
department,phone
{
Display: block;
color: #000000;
margin-left: 20pt;
}
  • The Staff list formatted with the CSS file

Chan Peter

Business Administration

22567123

Au Albert

Computing

22567321

 

 


Displaying XML with XSL

  • XSL (the eXtensible Stylesheet Language) is the preferred style sheet language of XML
  • XSL is to transform XML into HTML before it is displayed by the browser
  • Pure XML file employee-xsl.xml

<?xml version="1.0" encoding="ISO8859-1" ?>
<?xml:stylesheet type="text/xsl" href="employee.xsl" ?>
<Stafflist>
<employee>
<lastname>Chan</lastname>
<firstname>Peter</firstname>
<department>Business Administration </department>
<phone>22567123</phone>
</employee>
<employee>
<lastname>Au</lastname>
<firstname>Albert</firstname>
<department>Computing</department>
<phone>22567321</phone>
</employee>
</Stafflist>

<?xml version="1.0" ?>
<HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt; background-color:#EEEEEE">

<xsl:for-each select="Stafflist/employee">
<DIV STYLE="background-color:teal; color:white; padding:4px">
<SPAN STYLE="font-weight:bold; color:white">
<xsl:value-of select="lastname" />
</SPAN>
<xsl:value-of select="firstname" />
</DIV>

<DIV STYLE="margin-left:20px; margin-bottom:1em; font-size:10pt">
Department : <xsl:value-of select="department" />
<SPAN STYLE="font-style:italic">
, Phone number : <xsl:value-of select="phone" />
</SPAN>
</DIV>
</xsl:for-each>

</BODY>
</HTML>

 

  • The Staff list formatted with XSL style sheet


Role of XSL and CSS

To render XML documents

(1) if the document doesn't have to be transformed, use CSS, otherwise:
(2) use XSL-T, the transformation language of XSL, 
 (a)  generate the style properties together with the rearranged text, using a sub-language of XSL called XSL-FO (XSL Formatting Objects); or
 (b) generate a new XML or HTML document and provide a CSS style sheet for that new document.

Other related topics

  • Schemas
  • Document Object Model (DOM) - representation of an XML document as tree data structure
  • XML Parser DOM & SAX (Simple API for XML)
  • XPath - syntax for locating specific parts of an XML document
  • XSLT- XSL translation
  • XML tools

 Reference

www.w3c.org/XML

www.xml.org

www.w3schools.com/xml (XML tutorial sites)

XML parsers

xml.apache.org 

java.sun.com/xml