Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 7 XML

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

XML

Introduction to XML
Extensible Markup Language
We will learn:
• XML Introduction
• XML How to use
• XML Tree
• XML Syntax
• XML Elements
• XML Attributes
• XML and XSLT
What is XML
• XML stands for EXtensible Markup Language

• XML is a markup language much like HTML

• XML was designed to store and transport data

• XML is a software- and hardware-independent tool for


storing and transporting data.

• XML was designed to be self-descriptive

• XML is a W3C Recommendation


Example
• This note is a note to Tove, from Jani, stored as XML:

• The note is quite self-descriptive. It has sender and


receiver information. It also has a heading and a message
body.
Example
• XML is just information wrapped in tags. Someone must write
a piece of software to send, receive, store, or display it.
The Difference Between XML and HTML

XML and HTML were designed with different goals:


• XML was designed to carry data - with focus on what data
is
• HTML was designed to display data - with focus on how
data looks
• XML tags are not predefined like HTML tags are.
The Difference Between XML and HTML
• The tags in the previous example (like <to> and <from>) are
not defined in any XML standard.
• These tags are "invented" by the author of the XML
document.
• HTML works with predefined tags like <p>, <h1>, <table>, etc.

• With XML, the author must define both the tags and the
document structure.
XML is Extensible
• Most XML applications will work as expected even if new data
is added (or removed).
• Imagine an application designed to display the original version
of note.xml (<to> <from> <heading> <data>).
• Then imagine a newer version of note.xml with added <date>
and <hour> elements, and a removed <heading>.
• The way XML is constructed, older version of the application
can still work:
XML is Extensible
XML Simplifies Things

• It simplifies data sharing


• It simplifies data transport
• It simplifies platform changes
• It simplifies data availability
• XML stores data in plain text format. This provides a software-
and hardware-independent way of storing, transporting, and
sharing data.
• XML also makes it easier to expand or upgrade to new
operating systems, new applications, or new browsers, without
losing data.
How Can XML be Used?
XML separates data from presentation
-XML does not carry any information about how to
be displayed.
-The same XML data can be used in many
different presentation scenarios.
-Because of this, with XML, there is a full separation
between data and presentation.

XML is often a complement to HTML


-In many HTML applications, XML is used to store or
transport data, while HTML is used to format and display
the same data.
How Can XML be Used?
XML separates data from HTML
-When displaying data in HTML, you should not have to edit
the HTML file when the data changes.
-With XML, the data can be stored in separate XML files.
-With a few lines of JavaScript code, you can read an XML
file and update the data content of any HTML page.
Example: Books.xml
XML Tree Structure
• XML documents form a tree structure that starts at "the
root" and branches to "the leaves".

14
XML Tree Structure
• XML documents are formed as element trees.
• An XML tree starts at a root element and branches from the root
to child elements.
• All elements can have sub elements (child elements):

• The terms parent, child, and sibling are used to describe the
relationships between elements.
• All elements can have text content (Harry Potter) and
attributes (category="cooking").
XML Syntax Rules
• XML documents must have a root element.
• All XML elements must have a closing tag.
• XML tags are case sensitive.

• XML elements must be properly nested

Incorrect

• XML attribute values must be quoted

• Comments in XML
XML Syntax Rules
Entity References
• Some characters have a special meaning in XML.
Incorrect

• To avoid this error, replace the "<" character with an entity


reference:

• There are 5 pre-defined entity references in XML:


&lt; < less than
&gt; > greater than
&amp; & ampersand
&apos; ' apostrophe
&quot; " quotation mark
XML Elements
• An XML element is everything from (including) the
element's start tag to (including) the element's end tag.
• An element can contain: text, attributes, other elements, or a
mix of the above.
Empty XML Elements:
• An element with no content is said to be empty.

You can also use a so called self-closing tag:


XML Elements
XML Naming Rules:
• Element names are case-sensitive.
• Element names must start with a letter or underscore.
• Element names cannot start with the letters xml (or XML, or
Xml, etc).
• Element names can contain letters, digits, hyphens,
underscores, and periods.
• Element names cannot contain spaces.
XML Attributes
- XML elements can have attributes, just like HTML.
- Attributes are designed to contain data related to a specific
element.

- XML Elements vs. Attributes

Gender is an element Gender is an attribute.

- Attributes cannot contain multiple values (elements can)


- Attributes cannot contain tree structures (elements can)
XML Attributes for Metadata
• Sometimes ID references are assigned to elements. These
IDs can be used to identify XML elements in much the
same way as the id attribute in HTML.
XML and XSLT
• With XSLT you can transform an XML document into HTML.
• XSLT (eXtensible Stylesheet Language Transformations) is the
recommended style sheet language for XML.
• With XSLT you can add/remove elements and attributes to or
from the output file.
• You can also rearrange and sort elements, perform tests and
make decisions about which elements to hide and display, and a
lot more.
How to transform XML into XHTML using XSLT?
• The root element that declares the document to be an XSL style
sheet is <xsl:stylesheet> or <xsl:transform>.
Note: <xsl:stylesheet> and <xsl:transform> are completely
synonymous and either can be used!
• The correct way to declare an XSL style sheet according to the
W3C XSLT Recommendation is:

Or

• The xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
points to the official W3C XSLT namespace. If you use this
namespace, you must also include the attribute
version="1.0".
How to transform XML into XHTML using XSLT?
• We want to transform the following XML document
("cdcatalog.xml") into XHTML:
How to transform XML into XHTML using XSLT?
• You need to create an XSL Style Sheet ("cdcatalog.xsl") with
a transformation template:
How to transform XML into XHTML using XSLT?
• Link the XSL style sheet to the XML document by adding the
XSL style sheet reference to your XML document
("cdcatalog.xml"):
How to transform XML into XHTML using XSLT?
• If you have an XSLT compliant browser, it will nicely
transform your XML into XHTML.
• The results:

You might also like