XML
XML
XML
Example XML document:
XML Syntax
An XML document consists of
Markup
Tags, which begin with < and end with >
References, which begin with & and end with ;
The entities lt, gt, amp, apos, and quot are recognized in
every XML document.
Other XHTML entities, such as nbsp, are only recognized in
other XML documents if they are defined in the DTD
XML Syntax
Comments
CDATA section
XML Syntax
The CDATA section
XML Syntax
< and & must be represented by references
except
XML Syntax
Element tags and elements
Three types
XML Syntax
Start and empty-element tags may contain
attribute specifications separated by white
space
XML Syntax
Element and attribute names are case
sensitive
XML white space characters are space,
carriage return, line feed, and tab
XML Documents
A well-formed XML document
10
XML Documents
An XML document is written according to
an XML vocabulary that defines
11
XML Documents
12
XML Documents
13
XML Documents
Valid names and content for an XML
vocabulary can be specified using
Natural language
XML DTDs (Chapter 2)
XML Schema (Chapter 9)
14
XML Documents
Two types of XML parsers:
Validating
Requires document type declaration
Generates error if document does not
Non-validating
Checks for well-formedness
Can ignore external DTD
15
XML Documents
Good practice to begin XML documents with
an XML declaration
Minimal example:
If included, < must be very first character of the
document
To override default UTF-8/UTF-16 character
encoding, include encoding declaration
following version:
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jacksons slides
16
XML Documents
Internal subset of DTD
Declaration of
internal subset of DTD
17
XML Namespaces
XML Namespace: Collection of element and
attribute names associated with an XML vocabulary
Namespace Name: Absolute URI that is the name
of the namespace
18
XML Namespaces
Another form of xmlns attribute known as a
namespace declaration can be used to
associate a namespace prefix with a
namespace name:
Namespace prefix
Namespace declaration
19
XML Namespaces
Example use of namespace prefix:
20
XML Namespaces
In a namespace-aware XML application, all
element and attribute names are considered qualified
names
21
XML Namespaces
Other namespace usage:
A namespace can be declared and used on the same element
22
XML Namespaces
Other namespace usage:
23
24
25
Document
generated by
GET request to
VisitCountUpdate
servlet
26
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
XMLHttpRequest
request is processed
by doPost() method of
servlet
28
29
Function called as
state of connection
changes
30
Root of
returned
XML
document
31
(X)HTML
XML
CSS
JavaScript
JavaScript DOM (HTML and XML)
XMLHttpRequest in asynchronous mode
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jacksons slides
32
Java-based DOM
Java DOM API defined by org.w3c.dom
package
Semantically similar to JavaScript DOM
API, but many small syntactic differences
33
Java-based DOM
Methods such as
getElementsByTagName() return
instance of NodeList
34
Java-based DOM
Example: program to count link elements
in an RSS document:
35
Java-based DOM
Imports:
From Java
API for XML
Processing
(JAXP)
36
Java-based DOM
Default parser is non-validating and nonnamespace-aware.
Overriding:
Also setValidating(true)
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jacksons slides
37
Java-based DOM
Namespace-aware versions of methods end
in NS:
Namespace name
Local name
38
SAX
39
SAX
40
SAX
41
SAX
42
SAX
43
Transformations
JAXP provides API for transforming
between DOM, SAX, and Stream (text)
representations of XML documents
Example:
44
Transformations
45
Transformations
46
Transformations
SAX output means that a SAX event
handler is called:
47
XSL
The Extensible Stylesheet Language (XSL)
is an XML vocabulary typically used to
transform XML documents from one form to
another form
XSL document
Input XML
document
XSLT Processor
Output XML
document
48
XSL
XSL
markup
49
XSL
Input XML document HelloWorld.xml:
50
XSL
51
XSL
Components of XSL:
52
XPath
XPath operates on a tree representation of an
XML document
53
XPath
Location path: expression representing one
or more XPath tree nodes
Node test
54
XPath
XSLT specifies
context node
55
XPath
Node test:
Node-type test:
text(): true if node is a text node
comment(): true if node is a comment node
node(): true of any node
56
XPath
A location step can have one or more
predicates that act as filters:
This predicate
applied first
57
XPath
58
XPath
Abbreviations:
child::para = para
attribute::display = @display
parent::node() = ..
self::node() = .
59
XPath
A location path is one or more location steps
separated by /
60
XPath
Evaluating a two-step location path:
61
XPath
62
XPath
An absolute location path begins with / and uses
the document root as the context node
Ex: body/para
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jacksons slides
63
XPath
Another abbreviation:
/descendant-or-self::node()/ = //
Examples:
//strong: All strong elements in document
.//strong: All strong elements that are
descendants (or self) relative to the context node
64
XPath
Combining node lists:
65
XSLT
Template
rule
66
XSLT
XSLT processor deals with three XPath
trees:
67
XSLT
XSLT processing (high level):
68
XSLT
69
XSLT
70
XSLT
Restrictions on XPath in template rule
pattern (value of match attribute):
71
XSLT
72
XSLT
73
XSLT
74
XSLT
75
XSLT
Adding attributes:
76
XSLT
Source document elements are in a namespace
We want to copy
all h1 elements
plus all of their
descendants
77
XSLT
Copy
element
and content
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jacksons slides
78
XSLT
Template markup
in the result tree becomes
79
XSLT
Adding XML special characters to the result
Template:
Becomes & on input
Result:
disable-output-escaping also
applies to value-of
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jacksons slides
80
XSLT
Output formatting:
Add xml:space=preserve to
transform element of template to retain white
space
Use xsl:output element:
81
82
83