Lecture E-Commerce - Chapter 19: eXtensible Markup Language (XML) (part II). In this chapter students will be able to: XML elements are extensible, XML attributes, XML namespaces, XML encoding, viewing XML files.
Trang 1CSC 330 E-Commerce
Teacher
Ahmed Mumtaz Mustehsan
GM-IT CIIT Islamabad
COMSATS Institute of Information Technology
T2-Lecture-4
Trang 2eXtensible Markup Language (XML)
Part - II
For Lecture Material/Slides Thanks to: www.w3schools.com
Trang 4XML Elements are Extensible
XML elements can be extended to carry more information.
Look at the following XML example:
Trang 5XML Elements are Extensible…
Imagine that the author of the XML document added some extra information to it:
Should the application break or crash?
No The application should still be able to find the <to>,
<from>, and <body> elements in the XML document and produce the same output.
One of the beauty of XML, is that it can be extended
Message To: Mumtaz From: Tariq Don't forget to attend party on weekend!
Trang 6XML Attributes
Trang 7<file type="gif">computer.gif</file>
important to the software that wants to manipulate the element:
Trang 8XML Attributes Must be Quoted
single or double quotes can be used
Trang 9XML Attributes Must be Quoted
can use single quotes
Example:
<University head = ‘ Rector “COMSATS" Institute'>
or you can use character entities:
<University head = ‘ Rector "COMSATS" Institute'>
Trang 10XML Elements vs Attributes
Example-1:
<person sex="female“ >
<firstname> Amna </firstname>
<lastname> Atif </lastname>
Trang 11XML Elements vs Attributes…
In Example-1, sex is an attribute
In Example-2, sex is an element
attributes or when to use elements,
However:
Recommendations are:
Trang 12XML: Different ways to define information
presented in different ways
Trang 13XML: Different ways to define information.
Trang 14XML: Different ways to define information.
Trang 15Avoid XML Attributes?
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
Recommendations:
Use elements for data
Use attributes for information that is not relevant to the data.
Don't end up like this:
◦ <note day="10" month="01" year="2008"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>
Trang 16XML Attributes for Metadata
Sometimes ID references are assigned to elements.
These IDs can be used to identify XML elements the same way as the id attribute in HTML
Trang 17XML Attributes for Metadata…
notes
Recommendations:
Metadata (data about data) should be stored as
attributes, and the data itself should be stored as
elements
Trang 18XML Namespaces
Trang 19Before we Begin: a review of URI
Uniform Resource Identifier (URI) :
characters which identifies an Internet Resource
Locator (URL) which identifies an Internet domain
address
Resource Name (URN)
In our examples we will only use URLs
Trang 20XML Namespaces
This often results in a conflict when trying to mix XML documents from different XML applications
Trang 21would be a name conflict Both contain a <table>
element, but the elements have different content and
meaning
A user or an XML application will not know how to
Trang 22Solving the Name Conflict Using a Prefix
Name conflicts in XML can easily be avoided using a name
In the example above, there will be no conflict because the two
<table> elements have different names But are we allowed to use prefix with a : ?
Trang 23XML Namespaces : The xmlns Attribute
When using prefixes in XML
start tag of an element
Example
<h:table xmlns:h="http://www.w3.org/TR/html4/">
Trang 24XML Namespaces - The xmlns Attribute
The namespace declaration as an attribute can be defined:
Example-1: each attribute is defined with separate xmsns
Trang 25XML Namespaces - The xmlns Attribute…
The namespace declaration as an attribute can be defined:
Example-1: each attribute is defined with combined xmsns defination
The purpose is to give the namespace a unique name However, often
companies use the namespace as a pointer to a web page containing
Trang 26Default Namespaces
Defining a default namespace for an element saves us from using prefixes in all the child elements It has the following syntax:
<width>80</width>
<length>120</length>
</table>
Trang 27XML Encoding
Trang 28XML Encoding
like Norwegian æøå, or French êèé
or save the XML files as UTF-8
for each different character used in a document
called character set, character map, code set, and code page
Trang 29of text documents
by a name and a number
Unicode has two variants: UTF-8 and UTF-16
commonly used characters and two (or three) bytes for the rest
and three bytes for the rest
Trang 30UTF-8 - The Web Standard
HTML-5, CSS, JavaScript, PHP, SQL, and XML
Trang 31 The XML standard states that all XML software must
understand both UTF-8 and UTF-16.
UTF-8 is the default for documents without encoding
information.
In addition, most XML software systems understand
encodings like ISO-8859-1 , Windows-1252, and ASCII
Trang 32XML Errors
Most often,
three computers, the browser might display
meaningless text, or you might get an error message
Example:
“This XML file does not appear to have any style
information associated with it.”
Trang 33When you write an XML document:
standard
Trang 34Viewing XML Files
Trang 35Viewing XML Files
Raw XML files can be viewed in all major browsers.
Don't expect XML files to be displayed as HTML pages.
elements can be clicked to expand or collapse the element
structure To view the raw XML source (without the + and -
signs), select "View Page Source" or "View Source" from the browser menu.
raw XML, you must right click the page and select "View Source"
Trang 36Viewing an Invalid XML File
browsers might report the error, some may display
it incorrectly
Trang 37Why Does XML Display Like This?
to display the data
XML document, browsers do not know if a tag like
<table> describes an HTML table or a dining table
data, most browsers will just display the XML
document as it is
Trang 38The second line links the XML file to the CSS file:
Trang 39It is possible to use CSS to format an XML document.
to format an XML document:
The CD catalog formatted with the CSS file
Displaying your XML Files with CSS
Trang 41Displaying XML with XSLT
Transformations) is the recommended style sheet language for XML
elements to hide and display, etc
document
XML - XSLT
Trang 42XPath is Used in XSLT
XPath is a syntax for defining parts of an XML document
XPath uses path expressions to navigate in XML documents
XPath contains a library of standard functions
XPath is a major element in XSLT
XPath is also used in XQuery, XPointer and XLink
XML - Xpath
Trang 43XLink is used to create hyperlinks within XML
documents
link
links (for linking multiple resources together)
files
XML - XLink
Trang 44XPointer allows the links to point to specific parts of
Trang 45The End XML Part-II
T2-Lecture-4 Thank You