XML

From virtualMV2015wiki
Jump to: navigation, search
 Home  Main Page <

Introduction: Extensible Markup Language (XML)

The basic outline for these notes is based on a presentation by Daniel Rutten, from Clockwise Smart Software Solutions, to the Napier, (New Zealand) Microsoft Dot net user group in 2009.Based on a presentation by Daniel Rutten (2009)[1].

Icon Objective.png

Objective XML

By the end of this page you will be able to:

  • Describe XML
  • Style an XML file using CSS

Basic XML file

 <?xml version="1.0" encoding="ISO-8859-1"?>
 <people>
  <person>
    <LastName>Fant</LastName>
    <FirstName>Elly</FirstName>
    <comment>Requires a very large grey coat</comment>
  </person>
  <person>
    <LastName>Raff</LastName>
    <FirstName>Gee</FirstName>
  </person>
  <person>
    <LastName>Key</LastName>
    <FirstName>Mon</FirstName>
  </person>
 </people>

Note how <comment> is not mandatory

XML and Related Technologies

  • XML, XHTML , XML DOM,
  • XSL, XSLT
  • XPath, XSL-FO, XLink, XPointer, DTD
  • XSD, XForms, XQuery, SOAP, WSDL
  • RDF, RSS, WAP, SMIL, SVG

Brief history

SGML

  • 1960’s
  • Standard Generalized Markup Language
  • Manage large documents
  • Subject to frequent revisions
  • Printed in different formats
  • Specifies rules for tagging elements
  • SGML ISO-8879, 1986

XML

  • Derived from SGML
  • eXtensible Markup Language
  • Easier to implement but less flexible
  • No need for a Document Type Definition
  • W3C Recommendation in 1998

EXI (Efficient XML Interchange)

The World Wide Web Consortium (W3C) has issued a standard for a compact form of XML for use on smartphones and devices with memory or bandwidth constraints. The Efficient XML Interchange (EXI) standard will reduce XML processing's demands for network utilization, power, and energy. Work is already underway to support the standard for the compact representation of XML information in development toolkits. "[EXI is] an optimization of the XML exchange," says John Schneider, editor of the EXI specification. EXI could be used in sensor networks, cameras, automobiles, real-time trading systems, and wireless phones. "Even though [XML is] used everywhere, it's not used in some places because [of] constraints like giant documents or battery consumption or limited bandwidth," says W3C's Liam Quin. EXI could potentially displace XML at some user sites, say W3C officials. (Krill, 2011, March 10)[2]

HTML versus XML

  • HTML
    • Designed to display data
    • Focus on how data looks
    • Fixed tag set and semantics
  • XML
    • Designed to describe data
    • Focus on what data is
    • Extensible(!) tag set and semantics

Six golden rules of XML

1. All XML documents begin with

<?xml version="1.0" encoding="…"?>

2. All XML documents must have one(and only one) root element

3. All XML elements must have a closing tag

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

4. XML tags are case sensitive

<name> and <NAME> are not the same

5. All XML elements must be properly nested

Your <b><i>yuck</b></i> days are over

6. Attribute values must always be quoted

<note date="25 may 2006"></note>

Well-formed versus Valid

  • A "Well Formed" XML document has correct XML syntax
  • A "Valid" XML document also conforms to a DTD or XSD

Some features

  • Comments
  <!– This is a comment -->
  • Adding data that is not to be parsed (e.g. inserting HTML into an XML file
  <![CDATA[ no parsing here ]]>
  • White space
  • Namespaces: Allow you to cope with merging two XML files which have the same tags but for different purposes (e.g. Merging a Staff file with a student file.
  • Escape Characters
    &lt; < less than
    &gt; > greater than
    &amp; & ampersand
    &apos; ' apostrophe
    &quot; " quotation mark

Applying a CSS to an XML file

sample.xml

If we create an XML file as

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="sample.css"?><people>
  <person>
    <LastName>Fant</LastName><FirstName>Elly</FirstName>
    <comment>Trunk line stomp</comment>
  </person>
<person>
    <LastName>Lyon</LastName><FirstName>Cee</FirstName>
</person>
</people>

sample.css

We can build a CSS to style the XML file as follows:

people     /* styles for xml definitions  */ 
{ background-color: #ffffff; width: 100%; }
person
{ display: block; color: #FF0000; font-size: 15pt; }
LastName
{ color: #3333CC; font-size: 12pt; }
FirstName
{ color: #33CC33; font-size: 20pt; }
comment
{display: block; color: #000000; font-size: 10pt; margin-left: 20pt; }

Note: The display:block style forces the next element to be on a newline.

sample.html

Now if we create a container page, where we include an iFrame we can display the xml file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Assessment 1</title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<style type="text/css" media="all">@import "sample.css";</style>
</head>
<body>
Sample of XML and CSS
<div id="bodyarea">
	<iframe src="sample.xml" width=800 height=200 frameborder=0 scrolling=yes></iframe></div>
</body>
</html>


Example :Click here to run sample.html. The diagram following shows the result in Google's chrome.

XML/CSS sample)

XML and HTML

Html can be included in an xml file by adding

xmlns:html="http://www.w3.org/1999/xhtml"

to the root element Using this technique allows us to for example display an image within an xml file.

So if we have an XML file (xmlImage.xml) as

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="xmlImage.css" type="text/css"?>
<data xmlns:html="http://www.w3.org/1999/xhtml">
<pic>
   <html:img src="fern.png" width="48" height="36"></html:img>
</pic>
<heading>Fern</heading> <name>NZ Silver Fern</name> 
<description>The Silver fern is used by many New Zaland sports teams ...
 </description>
<pic>
   <html:img src="Kowhai.png" width="48" height="48" />
</pic>
<heading>Kowhai</heading> <name>NZ Kowhai flower</name> 
<description> The Kowhai is a yellow flower that ...
</description>
</data>

And an associated style sheet (xmlImage.css)

/* CSS Document */
#contents { padding: 24px; background: #EBF5FC;	min-height: 300px; }
body      { font-family: verdana,sans-serif; background: #EDEBE3; color: #333;
	    padding: 16px 20px 48px 20px; margin: 0;}
h1        { font-family: trebuchet ms; 	font-weight: bold; color: #333; margin:0; }
 
/* CSS for XML */
heading      { font-size: 24pt; color: #FF0000; }
name         { color: #0000FF; font-size: 12pt; }
description  { display: block; color: #000; margin-left: 12pt; margin-left: 60px; }

If we put this into an iframe page for display (xmlImage.html)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>XML Image Example</title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<link rel="stylesheet" type="text/css" href="xmlImage.css">
</head>
<body>
  <h1>XML Image Example</h1>
  <div id="main">
	<div id="contents">
  	  <p class="note">XML Generated information using an XML File and CSS</p>
      <iframe src="xmlImage.xml" width="100%" height="440px" scrolling="no" frameborder="0">
      </iframe>
	</div>
  </div>
</body>
</html>

Assuming the images are available, this would render in the web browser ...

Example :Click here to run xmlImage.htm. The diagram following shows the result in Google's chrome.

XML/CSS image example)


For more information refer to (Cormia, 2011)[3]

Examples

Resources

Icon References.png References

  1. Rutten, D. (2009) An introduction to XML and XSLT. Clickwise Smart Software Solutions. To Dot Net User Group, Napier, New Zealand [PPT]
  2. Krill, P. (2011, March 10) W3C Issues Compact XML Standard. InfoWorld. Retrieved from http://www.infoworld.com/d/application-development/w3c-issues-compact-xml-standard-198
  3. Cormia, R. (2011) COIN78 - XML Lesson 5: Using CSS with XML. Retrieved from http://fgamedia.org/faculty/rdcormia/COIN78/lesson05.htm
  4. Scalable Vector Graphics. (2010, March 20). In Wikipedia, The Free Encyclopedia. Retrieved 10:59, March 20, 2010, from http://en.wikipedia.org/w/index.php?title=Scalable_Vector_Graphics
  5. XML Course Tutorials and Reference Guide (2011). Retrieved from http://www.developphp.com/list_xml.php

XML. (2017). In virtualMV's ( Michael Verhaart ) wiki. Retrieved June 25, 2017, from http://www.virtualmv.com/wiki/index.php?title=XML    (zotero)