SE 5145 – eXtensible Markup Language (XML ) Introduction - I 2011-12/Spring, Bahçeşehir...
-
Upload
krista-piercefield -
Category
Documents
-
view
217 -
download
2
Transcript of SE 5145 – eXtensible Markup Language (XML ) Introduction - I 2011-12/Spring, Bahçeşehir...
SE 5145 – eXtensible Markup Language (XML )
Introduction - I
2011-12/Spring, Bahçeşehir University, Istanbul
What is XML?
eXtensible Markup Language Became a W3C Recommendation in 1998 A framework for defining markup languages Tag-based syntax, very much like HTML No fixed collection of markup tags Enables making up your own tags Each XML language targeted for application All XML languages share features Foundation for several next-gen Web technologies
XHTML, RSS (Blogs), AJAX, Web Services
2
Why Use XML ? (Advantages)
Represent semi-structured data (data that are structured, but don’t fit relational model)
XML is more flexible than DBs XML can be used as a way to interchange data between
disparate systems that were never designed to do so (e.g. hospitals x banks, etc.)
You get a massive infrastructure for free XML documents can be modularized. Parts can be
reused. Content is kept separate from any notion of presentation
3
Why Use XML ? (Advantages)
XML is text (Unicode) based. Takes up less space. Can be transmitted efficiently.
One XML document can be displayed differently in different media. Html, video, CD, DVD, You only have to change the XML document in order to
change all the rest. Information can be easily read and understood XML is an open format that can be processed by any
XML-aware app. like browser, word proc., spreadh.,etc
4
Potential Drawbacks of XML
5
6
HTML and XML, I
HTML and XML look similar, because they are both SGML languages (SGML = Standard Generalized Markup Language) Both HTML and XML use elements enclosed in tags (e.g.
<body>This is an element</body>) Both use tag attributes (e.g.,
<font face="Verdana" size="+1" color="red">)
Both use entities (<, >, &, ", ')
More precisely, HTML is defined in SGML XML is a (very small) subset of SGML
7
HTML and XML, II
HTML is used to mark up text so it can be displayed to users
XML is used to mark up data so it can be processed by computers
HTML describes both structure (e.g. <p>, <h2>, <em>) and appearance (e.g. <br>, <font>, <i>)
XML describes only content, or “meaning”
HTML uses a fixed, unchangeable set of tags
In XML, you make up your own tags
HTML is a markup language for a specific purpose (display in browsers)
XML is a framework for defining markup languages
8
HTML and XML, III
HTML is for humans HTML describes web pages You don’t want to see error messages about the web pages you
visit Browsers ignore and/or correct as many HTML errors as they can,
so HTML is often sloppy XML is for computers
XML describes data The rules are strict and errors are not allowed
In this way, XML is like a programming language Current versions of most browsers can display XML
However, browser support of XML is spotty at best
9
Viewing XML
XML is designed to be processed by computer programs, not to be displayed to humans
Nevertheless, almost all current browsers can display XML documents They don’t all display it the same way They may not display it at all if it has errors For best results, update your browsers to the newest available
versions Remember:
HTML is designed to be viewed, XML is designed to be used
10
Extended document standards
You can define your own XML tag sets, but here are some already available: XHTML: HTML redefined in XML SMIL: Synchronized Multimedia Integration Language MathML: Mathematical Markup Language SVG: Scalable Vector Graphics DrawML: Drawing MetaLanguage ICE: Information and Content Exchange ebXML: Electronic Business with XML cxml: Commerce XML CBL: Common Business Library
11
Vocabulary
SGML: Standard Generalized Markup Language XML : Extensible Markup Language DTD: Document Type Definition element: a start and end tag, along with their contents attribute: a value given in the start tag of an element entity: a representation of a particular character or string PI: a Processing Instruction, to possibly be used by a
program that processes this XML namespace: a unique string that references a DTD well-formed XML: XML that follows the basic syntax rules valid XML: well-formed XML that conforms to a DTD
12
XML-related technologies
13
XML-related technologies
CSS (Cascading Style Sheets) describe how to display HTML or XML in a browser
DTD (Document Type Definition) and XML Schemas are used to define legal XML tags and their attributes for particular purposes
DOM (Document Object Model), SAX (Simple API for XML, and JAXP (Java API for XML Processing) are all APIs for XML parsing
Applications of XML XHTML CML – chemical markup language WML – wireless markup language ThML – theological markup language
<h3 class="s05" id="One.2.p0.2">Having a Humble Opinion of Self</h3> <p class="First" id="One.2.p0.3">EVERY man naturally desires knowledge <note place="foot" id="One.2.p0.4"> <p class="Footnote" id="One.2.p0.5"><added id="One.2.p0.6"> <name id="One.2.p0.7">Aristotle</name>, Metaphysics, i. 1. </added></p> </note>; but what good is knowledge without fear of God? Indeed a humble rustic who serves God is better than a proud intellectual who neglects his soul to study the course of the stars. <added id="One.2.p0.8"><note place="foot" id="One.2.p0.9"> <p class="Footnote" id="One.2.p0.10"> Augustine, Confessions V. 4. </p> </note></added> </p>
14
SUMMARY
WHAT WHO WHEN WHY HOW
15
WHAT
XML is a language for creating other languages! XML lets you define schemas for tag-based languages (ergo,
“markup language”) XML allows you to extend any existing language (schema) with
your own tags (ergo, “eXtensible”) Examples of XML schemas
financial transactions (stock transactions) business documents (purchase order, invoice) remote procedure calls (SOAP) configuration files (security, server properties)
16
WHO
17
WHEN The World Wide Web Consortium (W3C) formed an XML Working Group in
1996 with these design goals:
1. XML shall be straightforwardly usable over the Internet
2. XML shall support a wide variety of applications
3. XML shall be compatible with SGML
4. It shall be easy to write programs which process XML documents
5. The number of optional features in XML is to be kept to the absolute minimum, ideally zero
6. XML documents should be human-legible and reasonably clear
7. The XML design should be prepared quickly
8. The design of XML shall be formal and concise
9. XML documents shall be easy to create
10. Terseness in XML markup is of minimal importance
18
WHEN
Development started in 1996, 1st ed. was published in 1998, 2nd in 2000, 3rd in 2004, 4th in 2006, 5th in 2008
XML 1.0 became a standard (W3C recommendation) on 10 February 1998 effort grew out of experience with SGML cooperation between various W3C member organizations (including Microsoft, Sun, IBM,
HP, Adobe) XML 1.1 became a standard (W3C recommendation) on 4 February 2004
less rigid constraints on names addresses issues involving character sets
Fifth edition of “W3C Recommendation”: http://www.w3.org/TR/REC-xml
19
HOW
20
HOW
21
WHY: Application Integration
22
WHY: Application Integration
23
WHY: Application Integration
24
WHY: Platform-Independent Services
In the past, functionality was exposed to remote clients via various remote object standards COM/DCOM CORBA Java RMI
Using these remote objects required a significant investment in a platform
Web Services provide a new way to expose functionality use XML and XML data types for transport work with any platform provide a bridge to existing business services
25
SE 5145 – eXtensible Markup Language (XML )
Introduction - II
2011-12/Spring, Bahçeşehir University, Istanbul
1st Assignment Well-formed, valid, and schema-valid documents
1) Find two HTML documents (on a Web server), which are NOT well-formed XML & briefly explain the violations (If you can’ t find any, then find well-formed ones & modify them to make NON-well-formed ;)
2) Find two more documents which, while well-formed but not valid XML. Explain again the fragments of the document and the validity constraint which is violated by this document.
Submit hard copy on due date: Next lesson (2 weeks later) Randomly selected students will be asked to present the
solutions on the due date
27
Example: Describing Information
DATA LABELS (Info about Data)
28
Describing Information with XML
29
Real Life Example XML used in a Desktop App., Dreamweaver
C:\Program Files\Adobe\Adobe Dreamweaver CS5.5\configuration\Menus\menus.xml
30
Tools for working with XML
31
XML: Design Goals
Separate syntax from semantics to provide a common framework for structuring information
Allow tailor-made markup for any imaginable application domain
Support internationalization (Unicode) and platform independence
Be the future of (semi)structured information (do some of the work now done by databases)
32
Basic Structure
An XML document is an ordered, labeled tree character data leaf nodes contain the actual data (text
strings) element nodes, are each labeled with
a name (often called the element type), and a set of attributes, each consisting of a name and a value, can have child nodes
33
XML Trees
An XML document has a single root node. The tree is a general ordered tree.
A parent node may have any number of children. Child nodes are ordered, and may have siblings.
Preorder traversals are usually used for getting information out of the tree.
34
35
XML Semantics: a Tree !
<data> <person id=“o555” > <name> Mary </name> <address> <street>Maple</street> <no> 345 </no> <city> Seattle </city> </address> </person> <person> <name> John </name> <address>Thailand </address> <phone>23456</phone> </person></data>
<data> <person id=“o555” > <name> Mary </name> <address> <street>Maple</street> <no> 345 </no> <city> Seattle </city> </address> </person> <person> <name> John </name> <address>Thailand </address> <phone>23456</phone> </person></data>
data
Mary
person
person
name address
name address
street no city
Maple 345 Seattle
JohnThai
phone
23456
id
o555
Elementnode
Textnode
Attributenode
Order matters !!!
XML Example
36
XML Example
<chapter id="cmds"> <chaptitle>FileCab</chaptitle>
<para>This chapter describes the commands that manage the <tm>FileCab</tm>inet application.
</para>
</chapter>
37
38
Overview of XML rules
Start with <?xml version="1"?> XML is case sensitive There must be exactly single root element (tag) that encloses all
the rest of the XML Tags are enclosed in angle brackets and can’t contain ‘<‘ or ‘&’. Tags come in pairs with start-tags and end-tags. Tags must be properly nested.
<name><email>…</name></email> is not allowed. <name><email>…</email><name> is.
Tags that do not have end-tags must be terminated by a ‘/’. <br /> is an html example.
Attribute values must be enclosed in double or single quotation marks
39
Overall structure
An XML document may start with one or more processing instructions (PIs) or directives: <?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="ss.css"?>
Following the directives, there must be exactly one tag, called the root element, containing all the rest of the XML: <weatherReport>
...</weatherReport>
40
Example XML document
<?xml version="1.0"?><weatherReport> <date>7/14/97</date> <city>North Place</city>, <state>NX</state> <country>USA</country> High Temp: <high scale="F">103</high> Low Temp: <low scale="F">70</low> Morning: <morning>Partly cloudy, Hazy</morning> Afternoon: <afternoon>Sunny & hot</afternoon> Evening: <evening>Clear and Cooler</evening></weatherReport>
From: XML: A Primer, by Simon St. Laurent
41
XML building blocks
Aside from the directives, an XML document is built from: elements: high in <high scale="F">103</high> tags, in pairs: <high scale="F">103</high> attributes: <high scale="F">103</high> entities: <afternoon>Sunny & hot</afternoon> character data, which may be:
parsed (processed as XML)--this is the default unparsed (all characters stand for themselves)
42
Basic XML Rules
43
Proper XML Syntax
44
Valid XML
You can make up your own XML tags and attributes, but... ...any program that uses the XML must know what to expect!
A DTD (Document Type Definition) defines what tags are legal and where they can occur in the XML
An XML document does not require a DTD XML is well-structured if it follows the rules given earlier In addition, XML is valid if it declares a DTD and conforms to
that DTD A DTD can be included in the XML, but is typically a separate
document Errors in XML documents will stop XML programs Some alternatives to DTDs are XML Schemas and RELAX NG
45
Well-Formed Documents
An XML document is said to be well-formed if it follows all the rules.
An XML parser is used to check that all the rules have been obeyed.
All of the browsers as Internet Explorer, Firefox, Safar Opera and Chrome come with XML parsers.
Parsers are also available for free download over the Internet. One is Xerces, from the Apache open-source project.
Java 1.4 and later versions also support an open-source parser. PHP and Javascript also have XML processing capabilities.
46
Well-structured XML example
<novel> <foreword> <paragraph> This is the great American novel. </paragraph></foreword> <chapter number="1"> <paragraph>It was a dark and stormy night. </paragraph> <paragraph>Suddenly, a shot rang out! </paragraph> </chapter></novel>
47
Contents of XML Files
48
The XML Declaration
49
Encoding XML (like Java) uses Unicode to encode characters. Unicode comes in many flavors. The most common one used in the world is UTF-8. It is backward compatible with ASCII UTF-8 is a variable length (multi byte) code. Characters are encoded in 1 byte, 2
bytes, or 4 bytes. The first 128 characters in Unicode are ASCII. In UTF-8, the numbers between 128 and 255 code for some of the more common
characters used in western Europe, such as ã, á, å, ç. Two byte codes are used for some characters not listed in the first 256 and some Asian
ideographs. Four byte codes can handle any ideographs that are left. Those using non-western languages should investigate other versions of Unicode (like
UTF-16, UTF-32). UTF-8 encodes each of the 1,112,064 code points in the Unicode character set using
one to four 8-bit bytes (termed “octets” in the Unicode Standard).
51
Names in XML
Names (as used for tags and attributes) must begin with a letter or underscore, and can consist of: Letters, both Roman (English) and foreign Digits, both Roman and foreign . (dot) - (hyphen) _ (underscore) : (colon) should be used only for namespaces Combining characters and extenders (not used in English)
52
Elements (Tags)
Elements
Elements are denoted by markup tags <foo attr1=“value” … > thetext </foo> Element start tag: foo Attribute: attr1 The character data: thetext Matching element end tag: </foo>
53
54
Attributes
(Exception: by using diff. namespaces)
55
Elements and attributes Attributes and elements are somewhat interchangeable Example using just elements:
<name> <first>David</first> <last>Matuszek</last></name>
Example using attributes: <name first="David" last="Matuszek"></name>
You will find that elements are easier to use in your programs--this is a good reason to prefer them
Attributes often contain metadata, such as unique IDs Generally speaking, browsers display only elements (values
enclosed by tags), not tags and attributes
56
Comments
57
Comments
<!-- This is a comment in both HTML and XML --> Comments are useful for:
Explaining the structure of an XML document Commenting out parts of the XML during development and testing
Comments are not elements and do not have an end tag The blanks after <!-- and before --> are optional The character sequence -- cannot occur in the comment The closing bracket must be --> Comments are not displayed by browsers, but can be seen by
anyone who looks at the source code
58
Character Data Sections
59
CDATA By default, all text inside an XML document is parsed You can force text to be treated as unparsed character data by
enclosing it in <![CDATA[ ... ]]> Any characters, even & and <, can occur inside a CDATA Whitespace inside a CDATA is (usually) preserved The only real restriction is that the character sequence ]]>
cannot occur inside a CDATA CDATA is useful when your text has a lot of illegal characters
(for example, if your XML document contains some HTML text)
60
Processing Instructions PIs (Processing Instructions) may occur anywhere in the XML document (but usually first) A PI is a command to the program processing the XML document to handle it in a certain way XML documents are typically processed by more than one program THESE INSTRUCTIONS ARE PARSER-SPECIFIC !
61
Entities
62
Entities
Five special characters must be written as entities: & for & (almost always necessary) < for < (almost always necessary) > for > (not usually necessary) " for " (necessary inside double quotes) ' for ' (necessary inside single quotes)
These entities can be used even in places where they are not absolutely required
These are the only predefined entities in XML
63
XML Namespaces
64
XML Namespaces
65
Namespaces
Recall that DTDs are used to define the tags that can be used in an XML document
An XML document may reference more than one DTD Namespaces are a way to specify which DTD defines a
given tag XML, like Java, uses qualified names
This helps to avoid collisions between names Java: myObject.myVariable XML: myDTD:myTag Note that XML uses a colon (:) rather than a dot (.)
66
Namespaces and URIs
A namespace is defined as a unique string To guarantee uniqueness, typically a URI (Uniform
Resource Indicator) is used, because the author “owns” the domain
It doesn't have to be a “real” URI; it just has to be a unique string
Example: http://www.matuszek.org/ns
There are two ways to use namespaces: Declare a default namespace Associate a prefix with a namespace, then use the prefix
in the XML to refer to the namespace
67
Namespace syntax In any start tag you can use the reserved attribute name xmlns:
<book xmlns="http://www.matuszek.org/ns"> This namespace will be used as the default for all elements up to the
corresponding end tag You can override it with a specific prefix
You can use almost this same form to declare a prefix: <book xmlns:dave="http://www.matuszek.org/ns"> Use this prefix on every tag and attribute you want to use from this
namespace, including end tags--it is not a default prefix <dave:chapter dave:number="1">To
Begin</dave:chapter>
You can use the prefix in the start tag in which it is defined: <dave:book xmlns:dave="http://www.matuszek.org/ns">
68
White Space Handling
69
White Space Handling
SE 5145 – eXtensible Markup Language (XML )
Introduction – III (XPath & XSLT)
2011-12/Spring, Bahçeşehir University, Istanbul
71
XPath Introduction
72
Important XPath Concepts
73
XPath Introduction
«Location step»
html/head/title html/body/p[1] html/body/p (3 nodes)
74
Some XPath Examples
75
Some XPath Examples
AltovaXMLSpy > Evaluate XPath..
businesscard.xml
/
BusinessCard
BusinessCard/Name
BusinessCard/Name/text()
BusinessCard/phone
BusinessCard/phone[2]
BusinessCard/phone[last()]
76
Starting to Work with XML
Your first XML file Associating an XML file with a CSS stylesheet Styling XML tags with CSS Working with XML and CSS2
77
Your first XML file
<?xml version="1.0" encoding="utf-8"?>
<FirstTag>
This is our first XML file
<!-- This is a comment -->
</FirstTag>
TRY TO OPEN THE FILE WITH A BROWSER, WHAT’S THE PROBLEM ?
78
Associating an XML file with a CSS stylesheet
CSS:
FirstTag
{
display : block;
font-family : Arial;
font-size : large;
color : Blue;
}
XML Updated:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="FirstXMLFile.css"?>
<FirstTag>
This is our first XML file
<!-- This is a comment -->
</FirstTag>
79
Styling XML tags with CSS
businesscard_nostyle.xml
businesscard.css
businesscard_style.xml(<?xml-stylesheet type="text/css" href="businesscard.css"?>)
CSS does not allow to display labels, but CSS2 does.
80
Working with XML and CSS2
businesscard_CSS2.css email:before {content: "e-mail: "}
phone:before {content: attr(type) ": "}
phone[primary]:after {content: " (" attr(primary) ")"}
businesscard_style_CSS2.xml<?xml-stylesheet type="text/css" href="businesscard_CSS2.css"?>
81
Working with XML and CSS2
82
Using XSLT
83
Creating XSLT Stylesheets
84
How XSLT Works
85
How XSLT Works
86
An Example XSLT Template
87
Enf of XML Fundamentals
Question ?