lpis.csd.auth.gr

Download Report

Transcript lpis.csd.auth.gr

ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ, ΑΠΘ
ΜΕΤΑΠΤΥΧΙΑΚΟ ΠΡΟΓΡΑΜΜΑ ΣΠΟΥΔΩΝ
Κατεύθυνση Πληροφοριακών Συστημάτων - 1ο Εξάμηνο
Σημασιολογικός Ιστός
lpis.csd.auth.gr/mtpx/sw/index.htm
Διδάσκων: Ν. Βασιλειάδης
Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ
Μαθήματα: 2-3-4
Chapter 2
Structured Web Documents in XML
Grigoris Antoniou
Frank van Harmelen
Introduction

Today HTML (HyperText Markup Language) is
the standard language for Web pages.

HTML was derived from SGML (Standard
Generalized Markup Language)
–
2-3
International standard (ISO 8879) for the definition
of device- and system-independent methods of
representing information, both human- and
machine-readable.
A Semantic Web Primer, 2nd Edition
Standards


2-4
Standards are important
–
They enable effective communication
–
They support technological progress and business
collaboration
In WWW, standards are set by the W3C
–
They are called recommendations
–
In a distributed environment without central
authority, standards cannot be enforced.
A Semantic Web Primer, 2nd Edition
SGML Applications

HTML
–

XML (eXtensible Markup Language)
–
2-5
Developed because SGML is too complex
for Internet-related purposes.
Its development was driven by
shortcomings of HTML.
A Semantic Web Primer, 2nd Edition
An HTML Example
<h2>Nonmonotonic Reasoning: ContextDependent Reasoning</h2>
<i>by <b>V. Marek</b> and
<b>M. Truszczynski</b></i><br>
Springer 1993<br>
ISBN 0387976892
2-6
A Semantic Web Primer, 2nd Edition
The Same Example in XML
<book>
<title>Nonmonotonic Reasoning: ContextDependent Reasoning</title>
<author>V. Marek</author>
<author>M. Truszczynski</author>
<publisher>Springer</publisher>
<year>1993</year>
<ISBN>0387976892</ISBN>
</book>
2-7
A Semantic Web Primer, 2nd Edition
HTML versus XML: Similarities
Both use tags (e.g. <h2> and </year>)
 Tags may be nested (tags within tags)
 Human users can read and interpret
both HTML and XML representations
quite easily
… But how about machines?

2-8
A Semantic Web Primer, 2nd Edition
HTML versus XML: Differences

All tags in XML must be closed
–
–


In HTML some tags (<br>), may be left open.
XHTML brings HTML more in line with XML
–
–
2-9
E.g. for an opening tag <title> there must be a closing
tag </title>
The enclosed content, together with its opening and
closing tags, is called element
Opening and closing tags in XHTML are balanced
Any valid XHTML document is also a valid XML
document
A Semantic Web Primer, 2nd Edition
Problems with Automated
Interpretation of HTML Documents
An intelligent agent trying to retrieve the names
of the authors of the book
 Authors’ names could appear immediately
after the title
 or immediately after the word by
 Are there two authors?
 Or just one, called “V. Marek and M.
Truszczynski”?
2-10
A Semantic Web Primer, 2nd Edition
HTML vs XML: Structural Information


HTML documents do not contain structural
information: pieces of the document and their
relationships.
XML more easily accessible to machines:
–
–
–
2-11
Every piece of information is described.
Relations are also defined through the nesting
structure
E.g., <author> tags appear within <book> tags, so
they describe properties of the particular book.
A Semantic Web Primer, 2nd Edition
HTML vs XML: Structural Information (2)


A machine processing the XML document
would be able to deduce that
–
the author element refers to the enclosing book
element
–
rather than by proximity considerations
XML allows the definition of constraints on
values
–
2-12
E.g. a year must be a number of 4 digits
A Semantic Web Primer, 2nd Edition
HTML vs XML: Formatting

The HTML representation provides more than
the XML representation:
–


Τhe main use of an HTML document is to display
information: it must define formatting
XML: separation of content from display
–
–
2-13
The formatting of the document is also described
Same information can be displayed in different ways
Content may be used for purposes other than display
A Semantic Web Primer, 2nd Edition
HTML vs XML: Another Example


2-14
In HTML
<h2>Relationship matter-energy</h2>
<i> E = M × c2 </i>
In XML
<equation>
<meaning>Relationship matter energy</meaning>
<leftside> E </leftside>
<rightside> M × c2 </rightside>
</equation>
A Semantic Web Primer, 2nd Edition
HTML vs XML: Different Use of Tags





2-15
Both HTML docs have the same tags.
In XML completely different tags are used!
HTML tags define display: color, lists …
XML tags not fixed: user definable tags
XML meta markup language: language for
defining markup languages
A Semantic Web Primer, 2nd Edition
XML Vocabularies


Web applications must agree on common
vocabularies to communicate and collaborate
Communities and business sectors are
defining their specialized vocabularies
–
–
–
–
2-16
mathematics (MathML)
bioinformatics (BSML)
human resources (HRML)
…
A Semantic Web Primer, 2nd Edition
Data Exchange

The main use of XML today is as a uniform data
exchange format between applications.
–

Companies retrieve information from their partners, and
update their databases accordingly.
–
–
–
2-17
Rather than a document markup language.
If there is not an agreed common standard, then specialized
processing and querying software must be developed for each
partner separately
Technical overhead
The software must be updated every time a partner decides to
change its own database format.
A Semantic Web Primer, 2nd Edition
Lecture Outline
1.
2.
3.
Introduction
Detailed Description of XML
Structuring
a)
b)
4.
5.
6.
2-18
DTDs
XML Schema
Namespaces
Accessing, querying XML documents: XPath
Transformations: XSLT
A Semantic Web Primer, 2nd Edition
The XML Language
An XML document consists of
 a prolog
 a number of elements
 an optional epilog (not discussed)
2-19
A Semantic Web Primer, 2nd Edition
Prolog of an XML Document
The prolog consists of
 an XML declaration and
 an optional reference to external structuring
documents
<?xml version="1.0" encoding="UTF-16"?>
<!DOCTYPE book SYSTEM "book.dtd">
2-20
A Semantic Web Primer, 2nd Edition
XML Elements

The “things” the XML document talks about
–

E.g. books, authors, publishers
An element consists of:
–
an opening tag
–
the content
–
a closing tag
<lecturer>David Billington</lecturer>
2-21
A Semantic Web Primer, 2nd Edition
XML Elements (2)

Tag names can be chosen almost freely.

The first character must be a letter, an
underscore, or a colon

No name may begin with the string “xml” in
any combination of cases
–
2-22
E.g. “Xml”, “xML”
A Semantic Web Primer, 2nd Edition
Content of XML Elements

Content may be text, or other elements, or nothing
<lecturer>
<name>David Billington</name>
<phone> +61 − 7 − 3875 507 </phone>
</lecturer>

If there is no content, then the element is called
empty; it is abbreviated as follows:
<lecturer/>
2-23
for
<lecturer></lecturer>
A Semantic Web Primer, 2nd Edition
XML Attributes

An empty element is not necessarily
meaningless
–

It may have some properties in terms of attributes
An attribute is a name-value pair inside the
opening tag of an element
<lecturer name="David Billington"
phone="+61 − 7 − 3875 507"/>
2-24
A Semantic Web Primer, 2nd Edition
XML Attributes: An Example
<order
orderNo="23456“
customer="John Smith"
date="October 15, 2002">
<item itemNo="a528" quantity="1"/>
<item itemNo="c817" quantity="3"/>
</order>
2-25
A Semantic Web Primer, 2nd Edition
The Same Example without Attributes
2-26
<order>
<orderNo>23456</orderNo>
<customer>John Smith</customer>
<date>October 15, 2002</date>
<item>
<itemNo>a528</itemNo>
<quantity>1</quantity>
</item>
<item>
<itemNo>c817</itemNo>
<quantity>3</quantity>
</item>
</order>
A Semantic Web Primer, 2nd Edition
XML Elements vs Attributes
2-27

Attributes can be replaced by elements

When to use elements and when
attributes is a matter of taste

But attributes
–
Cannot be nested
–
Cannot have two attributes with the same name in
the same element
A Semantic Web Primer, 2nd Edition
Mixed Content
Elements with mixed content contain both text
and other elements at the same time
<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-0713</shipdate>.
</letter>

2-28
A Semantic Web Primer, 2nd Edition
Further Components of XML Docs


2-29
Comments
–
A piece of text that is to be ignored by parser
–
<!-- This is a comment -->
Processing Instructions (PIs)
–
Define procedural attachments
–
<?stylesheet type="text/css"
href="mystyle.css"?>
A Semantic Web Primer, 2nd Edition
Well-Formed XML Documents


Syntactically correct documents
Some syntactic rules:
–
–
–
Only one outermost element (called root element)
Each element contains an opening and a
corresponding closing tag
Tags may not overlap
<author><name>Lee Hong</author></name>
–
–
2-30
Attributes within an element have unique names
Element and tag names must be permissible
A Semantic Web Primer, 2nd Edition
The Tree Model of XML Documents:
An Example
2-31
<email>
<head>
<from name="Michael Maher"
address="[email protected]"/>
<to name="Grigoris Antoniou"
address="[email protected]"/>
<subject>Where is your draft?</subject>
</head>
<body>
Grigoris, where is the draft of the paper you promised
me last week?
</body>
</email>
A Semantic Web Primer, 2nd Edition
Root
A Tree Example
email
head
from
name
body
to
address
name
subject
address
Grigoris, where is
the draft of the
paper you promised
me last week?
Michael Maher
2-32
[email protected]
Grigoris Antoniou
[email protected]
Where is your
draft?
A Semantic Web Primer, 2nd Edition
The Tree Model of XML Docs

The tree representation of an XML document
is an ordered labeled tree:
–
–
–
–
2-33
There is exactly one root
There are no cycles
Each non-root node has exactly one parent
Each node has a label.
A Semantic Web Primer, 2nd Edition
The Tree Model of XML Docs (2)

Element nodes are different from attribute nodes
–
–
–
–

There is difference between:
–
–
–
2-34
The order of elements is important
… but the order of attributes is not important
Attribute nodes are always terminal
Element nodes may be terminal or inner nodes
the root of the tree (representing the XML document)
the root element (email)
This distinction is important for addressing and
querying XML documents.
A Semantic Web Primer, 2nd Edition
Lecture Outline
1.
2.
3.
Introduction
Detailed Description of XML
Structuring
a)
b)
4.
5.
6.
2-35
DTDs
XML Schema
Namespaces
Accessing, querying XML documents: XPath
Transformations: XSLT
A Semantic Web Primer, 2nd Edition
Why Structuring XML Documents?

An XML document is well-formed if it
respects certain syntactic rules.

These rules say nothing specific about the
structure of the document.

Imagine 2 applications that try to
communicate
–
2-36
They wish to use the same vocabulary
A Semantic Web Primer, 2nd Edition
Structuring XML Documents


Define all the element and attribute names
that may be used
Define the structure
–
–

2-37
what values an attribute may take
which elements may or must occur within other
elements, etc.
If such structuring information exists, the
document can be validated
A Semantic Web Primer, 2nd Edition
Structuring XML Documents (2)


2-38
An XML document is valid if
–
it is well-formed
–
respects the structuring information it uses
There are two ways of defining the structure
of XML documents:
–
DTDs (the older and more restricted way)
–
XML Schema (offers extended possibilities)
A Semantic Web Primer, 2nd Edition
DTD: Element Type Definition
<lecturer>
<name>David Billington</name>
<phone> +61 − 7 − 3875 507 </phone>
</lecturer>
DTD for above element (and all lecturer elements):
<!ELEMENT lecturer (name,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
2-39
A Semantic Web Primer, 2nd Edition
The Meaning of the DTD
2-40

The element types lecturer, name, and phone may
be used in the document

A lecturer element contains a name element and a
phone element, in that order (sequence)

A name element and a phone element may have
any content

In DTDs, #PCDATA is the only atomic type for
elements
A Semantic Web Primer, 2nd Edition
DTD: Disjunction in Element Type
Definitions

We express that a lecturer element contains either a
name element or a phone element as:
<!ELEMENT lecturer (name | phone)>

2-41
Both elements below are validated:
<lecturer>
<name>David Billington</name>
A lecturer with both
</lecturer>
name and phone is
not validated!
<lecturer>
<phone> +61 − 7 − 3875 507 </phone>
</lecturer>
A Semantic Web Primer, 2nd Edition
DTD: Element Type Definitions – Any
order

A lecturer element contains a name element and a phone
element in any order.
<!ELEMENT lecturer ((name,phone) | (phone,name))>

Both elements below are validated:
<lecturer>
<name>David Billington</name>
<phone> +61 − 7 − 3875 507 </phone>
</lecturer>
<lecturer>
<phone> +61 − 7 − 3875 507 </phone>
<name>David Billington</name>
</lecturer>
2-42
A Semantic Web Primer, 2nd Edition
Example of an XML Element
<order
orderNo="23456"
customer="John Smith"
date="October 15, 2002">
<item itemNo="a528" quantity="1"/>
<item itemNo="c817" quantity="3"/>
</order>
2-43
A Semantic Web Primer, 2nd Edition
The Corresponding DTD
<!ELEMENT order (item+)>
<!ATTLIST order
orderNo
ID
customer
CDATA
date
CDATA
<!ELEMENT item EMPTY>
<!ATTLIST item
itemNo
ID
quantity
CDATA
comments CDATA
2-44
#REQUIRED
#REQUIRED
#REQUIRED>
#REQUIRED
#REQUIRED
#IMPLIED>
A Semantic Web Primer, 2nd Edition
Comments on the DTD
2-45

The item element type is defined to be empty

+ (after item) is a cardinality operator:
–
?: appears zero times or once
–
*: appears zero or more times
–
+: appears one or more times
–
No cardinality operator means exactly once
A Semantic Web Primer, 2nd Edition
Comments on the DTD (2)


In addition to defining elements, we define
attributes
This is done in an attribute list containing:
–
–

2-46
Name of the element type to which the list applies
A list of triplets of attribute name, attribute type,
and value type
Attribute name: A name that may be used in
an XML document using a DTD
A Semantic Web Primer, 2nd Edition
DTD: Attribute Types

Similar to predefined data types, but limited selection

The most important types are

2-47
–
CDATA, a string (sequence of characters)
–
ID, a name that is unique across the entire XML document
–
IDREF, a reference to another element with an ID attribute
carrying the same value as the IDREF attribute
–
IDREFS, a series of IDREFs
–
(v1| . . . |vn), an enumeration of all possible values
Limitations: no dates, number ranges etc.
A Semantic Web Primer, 2nd Edition
DTD: Attribute Value Types

#REQUIRED
–

#IMPLIED
–

This specifies the default value for the attribute
#FIXED "value"
–
2-48
The appearance of the attribute is optional
"value"
–

Attribute must appear in every occurrence of the
element type in the XML document
Every element must have this attribute
A Semantic Web Primer, 2nd Edition
Referencing with IDREF and IDREFS
<!ELEMENT family (person*)>
<!ELEMENT person (name)>
<!ELEMENT name (#PCDATA)>
<!ATTLIST person id
ID
#REQUIRED
mother IDREF
#IMPLIED
father
#IMPLIED
IDREF
children IDREFS #IMPLIED>
2-49
A Semantic Web Primer, 2nd Edition
An XML Document Respecting the DTD
2-50
<family>
<person id="bob" mother="mary" father="peter">
<name>Bob Marley</name>
</person>
<person id="bridget" mother="mary">
<name>Bridget Jones</name>
</person>
<person id="mary" children="bob bridget">
<name>Mary Poppins</name>
</person>
<person id="peter" children="bob">
<name>Peter Marley</name>
</person>
</family>
A Semantic Web Primer, 2nd Edition
The email example
2-51
<email>
<head>
<from name="Michael Maher"
address="[email protected]"/>
<to name="Grigoris Antoniou"
address="[email protected]"/>
<subject>Where is your draft?</subject>
</head>
<body>
Grigoris, where is the draft of the paper you promised
me last week?
</body>
</email>
A Semantic Web Primer, 2nd Edition
The email example slightly modified
<email>
<head>
<from name="Michael Maher" address="mm@cs..."/>
<to name="Grigoris Antoniou" address="grigoris@cs..."/>
<subject>Where is your draft?</subject>
</head>
<body>
<text>Grigoris, where is the draft of the paper you promised
me last week?</text>
<attachment file=“draft.doc” encoding=“mime”/>
<attachment file=“contract.pdf” encoding=“mime”/>
</body>
</email>
2-52
A Semantic Web Primer, 2nd Edition
A DTD for an Email Element
<!ELEMENT email (head, body)>
<!ELEMENT head (from, to+, cc*, subject)>
<!ELEMENT from EMPTY>
<!ATTLIST from
name
CDATA #IMPLIED
address CDATA #REQUIRED>
<!ELEMENT to EMPTY>
<!ATTLIST to
name
CDATA #IMPLIED
address CDATA #REQUIRED>
2-53
A Semantic Web Primer, 2nd Edition
A DTD for an Email Element (2)
<!ELEMENT cc EMPTY>
<!ATTLIST cc
name CDATA #IMPLIED
address CDATA #REQUIRED>
<!ELEMENT subject (#PCDATA)>
<!ELEMENT body (text, attachment*)>
<!ELEMENT text (#PCDATA)>
<!ELEMENT attachment EMPTY>
<!ATTLIST attachment
encoding
(mime | binhex) "mime"
file
CDATA
#REQUIRED>
2-54
A Semantic Web Primer, 2nd Edition
Interesting Parts of the DTD

A head element contains (in that order):
–
–
–
–

zero or more cc elements
a subject element
In from, to, and cc elements
–
–
2-55
a from element
at least one to element
the name attribute is not required
the address attribute is always required
A Semantic Web Primer, 2nd Edition
Interesting Parts of the DTD (2)


A body element contains
–
a text element
–
possibly followed by a number of attachment
elements
The encoding attribute of an attachment
element must have either the value “mime” or
“binhex”
–
2-56
“mime” is the default value
A Semantic Web Primer, 2nd Edition
Remarks on DTDs

A DTD can be interpreted as an Extended BackusNaur Form (EBNF)
–
–

<!ELEMENT email (head, body)>
is equivalent to email ::= head body
Recursive definitions possible in DTDs
<!ELEMENT bintree ((bintree, root, bintree) | emptytree)>
<!ELEMENT root (#PCDATA)>
<!ELEMENT emptytree EMPTY>
2-57
A Semantic Web Primer, 2nd Edition
DTD for the “original” Email
example
<!ELEMENT email (head, body)>
…
<!ELEMENT body (#PCDATA)>
2-58
A Semantic Web Primer, 2nd Edition
DTD for Mixed Content
2-59
<letter> Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-0713</shipdate>.
</letter>
<!ELEMENT letter
(#PCDATA | name | orderid | shipdate)*>
<!ELEMENT cname (#PCDATA)>
<!ELEMENT orderid (#PCDATA)>
<!ELEMENT shipdate (#PCDATA)>
A Semantic Web Primer, 2nd Edition
What if the email body had mixed
content?
<email>
<head>
<from name="Michael Maher" address="mm@cs..."/>
<to name="Grigoris Antoniou"
address="grigoris@cs..."/>
<subject>Where is your draft?</subject>
</head>
<body>Grigoris, where is the draft of the paper you promised
me last week?
<attachment file=“draft.doc” encoding=“mime”/>
<attachment file=“contract.pdf” encoding=“mime”/>
</body>
</email>
2-60
A Semantic Web Primer, 2nd Edition
A DTD for Email with mixed content
<!ELEMENT email (head, body)>
…
<!ELEMENT body (#PCDATA | attachment)*>
<!ELEMENT attachment EMPTY>
<!ATTLIST attachment
encoding
(mime | binhex) "mime"
file
CDATA
#REQUIRED>
2-61
A Semantic Web Primer, 2nd Edition
Lecture Outline
1.
2.
3.
Introduction
Detailed Description of XML
Structuring
a)
b)
4.
5.
6.
2-62
DTDs
XML Schema
Namespaces
Accessing, querying XML documents: XPath
Transformations: XSLT
A Semantic Web Primer, 2nd Edition
XML Schema


Significantly richer language for defining the
structure of XML documents
Its syntax is based on XML itself
–

Reuse and refinement of schemas
–

Expand or restrict already existent data types
Sophisticated set of data types
–
2-63
not necessary to write separate tools
compared to DTDs (which only supports strings)
A Semantic Web Primer, 2nd Edition
XML Schema (2)

An XML schema is an element with an opening
tag like
<schema version="1.0"
xmlns="http://www.w3.org/2000/10/XMLSchema">
– Uses the schema found at the W3C Web site as a
basis on which new schemas can be built.

Structure of schema elements
–
2-64
Definitions of element and attribute types using data
types
A Semantic Web Primer, 2nd Edition
Element Types
<element name="email"/>
<element name="head" minOccurs="1"
maxOccurs="1"/>
<element name="to" minOccurs="1"/>
Cardinality constraints:
 minOccurs="x" (default value 1)
 maxOccurs="x" (default value 1)
–
2-65
Generalizations of *,?,+ offered by DTDs
A Semantic Web Primer, 2nd Edition
Attribute Types
<attribute name="id" type="ID"
use="required"/>
<attribute name="speaks" type="Language"
default="en"/>
 Existence: use="x", where x may be optional
or required
Incompatible
 Default value: default="x"
 Fixed value: fixed=“x"
2-66
A Semantic Web Primer, 2nd Edition
Data Types


2-67
There is a variety of built-in data types
–
Numerical data types: integer, Short etc.
–
String types: string, ID, IDREF, CDATA etc.
–
Date and time data types: time, Month etc.
There are also user-defined data types
–
simple data types, which cannot use elements or
attributes
–
complex data types, which can use these
A Semantic Web Primer, 2nd Edition
Data Types (2)

Complex data types are defined from already
existing data types by defining some attributes
(if any), and using:
–
–
–
2-68
sequence, a sequence of existing data type
elements (order is important)
all, a collection of elements that must appear
(order is not important)
choice, a collection of elements, of which one will
be chosen
A Semantic Web Primer, 2nd Edition
A Data Type Example
<complexType name="lecturerType">
<sequence>
<element name="firstname" type="string"
minOccurs="0" maxOccurs="unbounded"/>
<element name="lastname" type="string"/>
</sequence>
<attribute name="title" type="string"
use="optional"/>
<element name=“lecturer"
</complexType>
2-69
type="lecturerType"/>
A Semantic Web Primer, 2nd Edition
Data Type Extension

2-70
Already existing data types can be extended by new
elements or attributes. Example:
<complexType name="extendedLecturerType">
<complexContent>
<extension base="lecturerType">
<sequence>
<element name="email" type="string"
minOccurs="0" maxOccurs="1"/>
</sequence>
<attribute name="rank" type="string" use="required"/>
</extension>
<complexContent>
A Semantic Web Primer, 2nd Edition
</complexType>
Resulting Data Type
<complexType name="extendedLecturerType">
<sequence>
<element name="firstname" type="string"
minOccurs="0" maxOccurs="unbounded"/>
<element name="lastname" type="string"/>
<element name="email" type="string"
minOccurs="0" maxOccurs="1"/>
</sequence>
<attribute name="title" type="string" use="optional"/>
<attribute name="rank" type="string" use="required"/>
</complexType>
2-71
A Semantic Web Primer, 2nd Edition
Data Type Extension (2)

A hierarchical relationship exists
between the original and the extended
type
–
–
2-72
Instances of the extended type are also
instances of the original type
They may contain additional information,
but neither less information, nor information
of the wrong type
A Semantic Web Primer, 2nd Edition
Data Type Restriction

An existing data type may be restricted by
adding constraints on certain values
–
–

Restriction is not the opposite from extension
–

Elements or attributes cannot be deleted
Hierarchical relationship:
–
2-73
New type and use attributes may be added
minOccurs, maxOccurs may be tightened
–
Instances of restricted type are also instances of
original type
They satisfy at least the constraints of original type
A Semantic Web Primer, 2nd Edition
Example of Data Type Restriction
<complexType name="restrictedLecturerType">
<complexContent>
<restriction base="lecturerType">
<sequence>
<element name="firstname" type="string"
minOccurs="1" maxOccurs="2"/>
</sequence>
<attribute name="title" type=“string”
use="required"/>
</restriction>
</complexContent>
2-74 </complexType>
A Semantic Web Primer, 2nd Edition
Restriction of Simple Data Types
<simpleType name="dayOfMonth">
<restriction base="integer">
<minInclusive value="1"/>
<maxInclusive value="31"/>
</restriction>
</simpleType>
2-75
A Semantic Web Primer, 2nd Edition
Data Type Restriction: Enumeration
2-76
<simpleType name="dayOfWeek">
<restriction base="string">
<enumeration value="Mon"/>
<enumeration value="Tue"/>
<enumeration value="Wed"/>
<enumeration value="Thu"/>
<enumeration value="Fri"/>
<enumeration value="Sat"/>
<enumeration value="Sun"/>
</restriction>
</simpleType>
A Semantic Web Primer, 2nd Edition
The email example (again…)
<email>
<head>
<from name="Michael Maher" address="mm@cs..."/>
<to name="Grigoris Antoniou" address="grigoris@cs..."/>
<subject>Where is your draft?</subject>
</head>
<body>
<text>Grigoris, where is the draft of the paper you promised
me last week?</text>
<attachment file=“draft.doc” encoding=“mime”/>
<attachment file=“contract.pdf” encoding=“mime”/>
</body>
</email>
2-77
A Semantic Web Primer, 2nd Edition
XML Schema: The Email Example
<element name="email" type="emailType"/>
<complexType name="emailType">
<sequence>
<element name="head" type="headType"/>
<element name="body" type="bodyType"/>
</sequence>
</complexType>
2-78
A Semantic Web Primer, 2nd Edition
XML Schema: The Email Example (2)
<complexType name="headType">
<sequence>
<element name="from" type="nameAddress"/>
<element name="to" type="nameAddress"
minOccurs="1" maxOccurs="unbounded"/>
<element name="cc" type="nameAddress"
minOccurs="0" maxOccurs="unbounded"/>
<element name="subject" type="string"/>
</sequence>
</complexType>
2-79
A Semantic Web Primer, 2nd Edition
XML Schema: The Email Example (3)
<complexType name="nameAddress">
<attribute name="name" type="string"
use="optional"/>
<attribute name="address"
type="string" use="required"/>
</complexType>
2-80
A Semantic Web Primer, 2nd Edition
XML Schema: The Email Example (4)
2-81
<complexType name="bodyType">
<sequence>
<element name="text" type="string"/>
<element name="attachment" minOccurs="0"
maxOccurs="unbounded">
<complexType>
<attribute name="encoding" use=“optional" default ="mime">
<simpleType>
<restriction base="string">
<enumeration value="mime"/>
<enumeration value="binhex"/>
</restriction>
</simpleType>
</attribute>
<attribute name="file" type="string" use="required"/>
…
A Semantic Web Primer, 2nd Edition
Remarks for the e-Mail Example


Some data types are defined separately and
given names
Other data types are defined (anonymously)
within other types
–

If a type is only used once, it makes sense to
define it anonymously for local use.
–
2-82
E.g. the types for the attachment element and the
encoding attribute
However, this approach reaches its limitations
quickly if nesting becomes too deep.
A Semantic Web Primer, 2nd Edition
XML Schema: The Email Example (4a)
<complexType name="bodyType">
<sequence>
<element name="text" type="string"/>
<element name="attachment" type=“attachmentType”
minOccurs="0" maxOccurs="unbounded“/>
<sequence>
</complexType>
2-83
<complexType name=“attachmentType”>
<attribute name="encoding" type=“encodingType”
use=“optional" default ="mime“/>
<attribute name="file" type="string" use="required"/>
</complexType>
A Semantic Web Primer, 2nd Edition
XML Schema: The Email Example (4b)
<simpleType name=“encodingType”>
<restriction base="string">
<enumeration value="mime"/>
<enumeration value="binhex"/>
</restriction>
</simpleType>
2-84
A Semantic Web Primer, 2nd Edition
XML Schema for Mixed Content
<xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid"
type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element> <letter> Dear Mr.<name>John Smith</name>.
2-85
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
A Semantic Web Primer, 2nd Edition
Lecture Outline
1.
2.
3.
Introduction
Detailed Description of XML
Structuring
a)
b)
4.
5.
6.
2-86
DTDs
XML Schema
Namespaces
Accessing, querying XML documents: XPath
Transformations: XSLT
A Semantic Web Primer, 2nd Edition
Namespaces

An XML document may use more than one
DTD or schema

Since each structuring document was
developed independently, name clashes may
appear

The solution is to use a different prefix for
each DTD or schema
–
2-87
prefix:name
A Semantic Web Primer, 2nd Edition
An Example
<vu:instructors xmlns:vu="http://www.vu.com/empDTD"
xmlns:gu="http://www.gu.au/empDTD"
xmlns:uky="http://www.uky.edu/empDTD">
<uky:faculty uky:title="assistant professor"
uky:name="John Smith"
uky:department="Computer Science"/>
<gu:academicStaff gu:title="lecturer"
gu:name="Mate Jones"
gu:school="Information Technology"/>
</vu:instructors>
2-88
A Semantic Web Primer, 2nd Edition
Namespace Declarations

Namespaces are declared within an element
and can be used in that element and any of
its children (elements and attributes)

A namespace declaration has the form:
–
–

2-89
xmlns:prefix="location"
location is the address of the DTD or schema
If a prefix is not specified: xmlns="location"
then the location is used by default
A Semantic Web Primer, 2nd Edition
Namespace without prefix example
<vu:instructors xmlns:vu="http://www.vu.com/empDTD"
xmlns="http://www.gu.au/empDTD"
xmlns:uky="http://www.uky.edu/empDTD">
<uky:faculty uky:title="assistant professor"
uky:name="John Smith"
uky:department="Computer Science"/>
2-90
<academicStaff title="lecturer“
name="Mate Jones"
school="Information Technology"/>
</vu:instructors>
A Semantic Web Primer, 2nd Edition
Lecture Outline
1.
2.
3.
Introduction
Detailed Description of XML
Structuring
a)
b)
4.
5.
6.
2-91
DTDs
XML Schema
Namespaces
Accessing, querying XML documents: XPath
Transformations: XSLT
A Semantic Web Primer, 2nd Edition
Addressing and Querying XML
Documents

In relational databases, parts of a database can
be selected and retrieved using SQL
–
–

The central concept of XML query languages is
a path expression
–
2-92
Same necessary for XML documents
Query languages: XQuery, XQL, XML-QL
Specifies how a node or a set of nodes, in the tree
representation of the XML document can be reached
A Semantic Web Primer, 2nd Edition
XPath


XPath is core for XML query languages
Language for addressing parts of an XML
document.
–
–

XPath expressions can be used for purposes
other than querying
–
2-93
It operates on the tree data model of XML
It has a non-XML syntax
For transforming XML documents.
A Semantic Web Primer, 2nd Edition
Types of Path Expressions

Absolute (starting at the root of the
tree)
–
–

2-94
Syntactically they begin with the symbol /
It refers to the root of the document
(situated one level above the root element
of the document)
Relative to a context node
A Semantic Web Primer, 2nd Edition
An XML Example
2-95
<library location="Bremen">
<author name="Henry Wise">
<book title="Artificial Intelligence"/>
<book title="Modern Web Services"/>
<book title="Theory of Computation"/>
</author>
<author name="William Smart">
<book title="Artificial Intelligence"/>
</author>
<author name="Cynthia Singleton">
<book title="The Semantic Web"/>
<book title="Browser Technology Revised"/>
</author>
</library>
A Semantic Web Primer, 2nd Edition
Tree Representation
root
library
location
Bremen
2-96
author
author
author
name
book
book
book
name
book
name
book
book
Henry
Wise
title
title
title
William
Smart
title
Cynthia
Singleton
title
title
Artificial
Intelligence
Modern
Web
Services
Theory of
Computation
Artificial
Intelligen
ce
The
Semantic
Web
Browser
Technolo
gy
Revised
A Semantic Web Primer, 2nd Edition
Examples of Path Expressions in
XPath

Address all author elements
/library/author

Addresses all author elements that are
children of the library element node, which
resides immediately below the root
/t1/.../tn, where each ti+1 is a child node of ti,
is a path through the tree representation

2-97
A Semantic Web Primer, 2nd Edition
Examples of Path Expressions in
XPath (2)



2-98
Address all author elements
//author
Here // says that we should consider all
elements in the document and check
whether they are of type author
This path expression addresses all author
elements anywhere in the document
A Semantic Web Primer, 2nd Edition
Examples of Path Expressions in
XPath (3)

Address the location attribute nodes within
library element nodes
/library/@location

The symbol @ is used to denote attribute
nodes
2-99
A Semantic Web Primer, 2nd Edition
Examples of Path Expressions in
XPath (4)

Find if there are title attribute nodes within
book elements anywhere in the document,
which have the value “Artificial Intelligence”
//book/@title="Artificial Intelligence"
2100
A Semantic Web Primer, 2nd Edition
Tree Representation of Query 4
root
library
location
Bremen
2101
author
author
author
name
book
book
book
name
book
name
book
book
Henry
Wise
title
title
title
William
Smart
title
Cynthia
Singleton
title
title
Artificial
Intelligence
Modern
Web
Services
Theory of
Computation
Artificial
Intelligen
ce
The
Semantic
Web
Browser
Technolo
gy
Revised
Non-empty set;
return true
A Semantic Web Primer, 2nd Edition
Examples of Path Expressions in
XPath (5)


Address all books with title “Artificial
Intelligence”
//book[@title="Artificial Intelligence"]
Test within square brackets: a filter expression
–

Difference with query 4.
–
2102
It restricts the set of addressed nodes.
–
Q5 addresses book elements, the title of which
satisfies a certain condition.
Q4 tests if there are title attribute nodes of book
elements with a certain characteristic
A Semantic Web Primer, 2nd Edition
Tree Representation of Query 5
root
library
location
Bremen
2103
author
author
author
name
book
book
book
name
book
name
book
book
Henry
Wise
title
title
title
William
Smart
title
Cynthia
Singleton
title
title
Artificial
Intelligence
Modern
Web
Services
Theory of
Computation
Artificial
Intelligen
ce
The
Semantic
Web
A Semantic Web Primer, 2nd Edition
Browser
Technolo
gy
Revised
Examples of Path Expressions in
XPath (6)



2104
Address the first author element node in the
XML document
//author[1]
Address the last book element within the first
author element node in the document
//author[1]/book[last()]
Address all book element nodes without a title
attribute
//book[not(@title)]
A Semantic Web Primer, 2nd Edition
More XPath


2105
How can we address the first book of each
author?
//author/book[1]
The following will work as well
//book[1]
A Semantic Web Primer, 2nd Edition
General Form of Path Expressions
A path expression consists of a series
of steps, separated by slashes
 A step consists of

–
–
–
2106
An axis specifier,
A node test, and
An optional predicate
A Semantic Web Primer, 2nd Edition
General Form of Path Expressions (2)

An axis specifier determines the tree
relationship between the nodes to be
addressed and the context node
–
–
2107
E.g. parent, ancestor, child (the default),
sibling, attribute node
// is such an axis specifier: descendant or
self
A Semantic Web Primer, 2nd Edition
General Form of Path Expressions (3)

A node test specifies which nodes to
address
–
The most common node tests are element
names


–
2108
E.g. //author/book
E.g., * addresses all element nodes
comment() addresses all comment nodes
A Semantic Web Primer, 2nd Edition
General Form of Path Expressions (4)

Predicates (or filter expressions) are
optional and are used to refine the set of
addressed nodes
–
–
–

2109
E.g., the expression [1] selects the first node
[position()=last()] selects the last node
[position() mod 2 =0] selects the even nodes
XPath has a more complicated full syntax.
–
We have only presented the abbreviated syntax
A Semantic Web Primer, 2nd Edition
More XPath Examples (1)





2110
para selects the para element children of the
context node
* selects all element children of the context node
text() selects all text node children of the context
node
@* selects all the attributes of the context node
*/para selects all para grandchildren of the
context node
A Semantic Web Primer, 2nd Edition
More XPath Examples (2)



2111
/doc/chapter[5]/section[2] selects the 2nd
section of the 5th chapter of the doc
chapter//para selects the para element
descendants of the chapter element children
of the context node
//olist/item selects all the item elements in the
same document as the context node that have
an olist parent
A Semantic Web Primer, 2nd Edition
More XPath Examples (3)




2112
. selects the context node
.//para selects the para element descendants of
the context node
.. selects the parent of the context node
../@lang selects the lang attribute of the parent
of the context node
A Semantic Web Primer, 2nd Edition
More XPath Examples (4)


para[@type="warning"][5] selects the 5th para
child of the context node that has a type attribute
with value warning
para[5][@type="warning"] selects the 5th para
child of the context node if that child has a type
attribute with value warning
–
2113
Could be written also as:
para[position()=5 and @type="warning"]
Lectures 2 & 3: Information Sharing
on the Web: XML
More XPath Examples (5)
2114

chapter[title="Introduction"] selects the chapter
children of the context node that have one or more
title children with string-value equal to Introduction

chapter[title] selects the chapter children of the
context node that have one or more title children

employee[@secretary and @assistant] selects all
the employee children of the context node that
have both a secretary attribute and an assistant
attribute
Lectures 2 & 3: Information Sharing
on the Web: XML
Lecture Outline
1.
2.
3.
Introduction
Detailed Description of XML
Structuring
a)
b)
4.
5.
2115
6.
DTDs
XML Schema
Namespaces
Accessing, querying XML documents: XPath
Transformations: XSLT
A Semantic Web Primer, 2nd Edition
Displaying XML Documents



2116
So far we have not provided any information
about how XML documents can be
displayed.
Such information is necessary because
unlike HTML documents, XML documents do
not contain formatting information.
The advantage is that a given XML
document can be presented in various ways,
when different style sheets are applied to it.
A Semantic Web Primer, 2nd Edition
Displaying XML Documents
2117
<author>
<name>Grigoris Antoniou</name>
<affiliation>University of Bremen</affiliation>
<email>[email protected]</email>
</author>
may be displayed in different ways:
Grigoris Antoniou
Grigoris Antoniou
University of Bremen
University of Bremen
[email protected]
[email protected]
A Semantic Web Primer, 2nd Edition
Style Sheets

Style sheets can be written in various
languages
–
–

XSL includes
–
–
2118
E.g. CSS2 (cascading style sheets level 2)
XSL (extensible stylesheet language)
–
a transformation language (XSLT)
a formatting language
Both are XML applications
A Semantic Web Primer, 2nd Edition
XSL Transformations (XSLT)

XSLT specifies rules with which an input XML
document is transformed to
–
–
–


2119
another XML document
an HTML document
plain text
The output document may use the same DTD or
schema, or a completely different vocabulary
XSLT can be used independently of the
formatting language
A Semantic Web Primer, 2nd Edition
XSLT (2)




2120
Move data and metadata from one XML
representation to another
XSLT is chosen when applications that use different
DTDs or schemas need to communicate
XSLT can be used for machine processing of
content without any regard to displaying the
information for people to read.
In the following we use XSLT only to display XML
documents
A Semantic Web Primer, 2nd Edition
XSLT Transformation into HTML
2121
<xsl:template match="/author">
<html>
<head><title>An author</title></head>
<body bgcolor="white">
<b><xsl:value-of select="name"/></b><br>
<xsl:value-of select="affiliation"/><br>
<i><xsl:value-of select="email"/></i>
</body>
</html>
</xsl:template>
A Semantic Web Primer, 2nd Edition
Style Sheet Output
2122
<html>
<head><title>An author</title></head>
<body bgcolor="white">
<b>Grigoris Antoniou</b><br>
University of Bremen<br>
<i>[email protected]</i>
</body>
</html>
A Semantic Web Primer, 2nd Edition
Observations About XSLT

XSLT documents are XML documents
–

The XSLT document defines a template
–

2123
XSLT resides on top of XML
In this case an HTML document, with some
placeholders for content to be inserted
xsl:value-of retrieves the value of an element
and copies it into the output document
–
It places some content into the template
A Semantic Web Primer, 2nd Edition
A Template
2124
<html>
<head><title>An author</title></head>
<body bgcolor="white">
<b>...</b><br>
...<br>
<i>...</i>
</body>
</html>
A Semantic Web Primer, 2nd Edition
Auxiliary Templates
We have an XML document with details
of several authors
 It is a waste of effort to treat each author
element separately
 In such cases, a special template is
defined for author elements, which is
used by the main template

2125
A Semantic Web Primer, 2nd Edition
Example of an Auxiliary Template
2126
<authors>
<author>
<name>Grigoris Antoniou</name>
<affiliation>University of Bremen</affiliation>
<email>[email protected]</email>
</author>
<author>
<name>David Billington</name>
<affiliation>Griffith University</affiliation>
<email>[email protected]</email>
</author>
</authors>
A Semantic Web Primer, 2nd Edition
Desired Output
2127
A Semantic Web Primer, 2nd Edition
Example of an Auxiliary Template (2)
2128
<xsl:template match="/">
<html>
<head><title>Authors</title></head>
<body bgcolor="white">
<xsl:apply-templates select="authors"/>
<!-- Apply templates for AUTHORS
children -->
</body>
</html>
</xsl:template>
A Semantic Web Primer, 2nd Edition
Example of an Auxiliary Template (3)
2129
<xsl:template match="authors">
<xsl:apply-templates select="author"/>
</xsl:template>
<xsl:template match="author">
<h2><xsl:value-of select="name"/></h2>
Affiliation:<xsl:value-of select="affiliation"/><br>
Email: <xsl:value-of select="email"/>
<p>
</xsl:template>
A Semantic Web Primer, 2nd Edition
Multiple Authors Output
2130
<html>
<head><title>Authors</title></head>
<body bgcolor="white">
<h2>Grigoris Antoniou</h2>
Affiliation: University of Bremen<br>
Email: [email protected]
<p>
<h2>David Billington</h2>
Affiliation: Griffith University<br>
Email: [email protected]
<p>
</body>
</html>
A Semantic Web Primer, 2nd Edition
Explanation of the Example

xsl:apply-templates element causes all children of
the context node to be matched against the
selected path expression
–
–
–
2131
E.g., if the current template applies to /, then the element
xsl:apply-templates applies to the root element
I.e. the authors element (/ is located above the root
element)
If the current context node is the authors element, then
the element xsl:apply-templates select="author"
causes the template for the author elements to be
applied to all author children of the authors element
A Semantic Web Primer, 2nd Edition
Explanation of the Example (2)

It is good practice to define a template for each
element type in the document
–
–

2132
Even if no specific processing is applied to certain
elements, the xsl:apply-templates element should
be used
E.g. authors
In this way, we work from the root to the leaves
of the tree, and all templates are applied
A Semantic Web Primer, 2nd Edition
Processing XML Attributes

Suppose we wish to transform to itself the
element:
<person firstname="John" lastname="Woo"/>

2133
Wrong solution:
<xsl:template match="person">
<person
firstname="<xsl:value-of select="@firstname">"
lastname="<xsl:value-of select="@lastname">"/>
</xsl:template>
A Semantic Web Primer, 2nd Edition
Processing XML Attributes (2)


2134
Not well-formed because tags are not
allowed within the values of attributes
We wish to add attribute values into template
<xsl:template match="person">
<person firstname="{@firstname}"
lastname="{@lastname}"/>
</xsl:template>
A Semantic Web Primer, 2nd Edition
Transforming an XML Document to
Another
Root
Root
author
s
author
s
author
author
author
name
name
Grigoris
Antonio
u
2135
affiliati
on
email
name
affiliati
on
email
author
conta
ct
institut
e
email
Grigoris
Antonio
u
Universit
y of
Bremen
ga@tzi.
de
David
Billingto
n
Griffith
Universit
y
david@
gu.edu.
net
name
conta
ct
institut
e
email
Griffith
Universi
ty
david@
gu.edu.
net
David
Billingto
n
Universi
ty of
Bremen
ga@tzi.
de
A Semantic Web Primer, 2nd Edition
Transforming an XML Document to
Another (2)
<xsl:template match="/">
<?xml version="1.0" encoding="UTF-16"?>
<authors>
<xsl:apply-templates select="authors"/>
</authors>
</xsl:template>
<xsl:template match="authors">
<author>
<xsl:apply-templates select="author"/>
2</author>
136 </xsl:template>
A Semantic Web Primer, 2nd Edition
Transforming an XML Document to
Another (3)
2137
<xsl:template match="author">
<name><xsl:value-of select="name"/></name>
<contact>
<institution>
<xsl:value-of select="affiliation"/>
</institution>
<email><xsl:value-of select="email"/></email>
</contact>
</xsl:template>
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:for-each


Allows you to do looping in XSLT by selecting
every XML element of a specified node-set
Sometimes the same effect can be performed
using xsl:apply-templates, but not always
–
–
2138
When the “specified node-set” cannot be determined
using a proper X-Path expression, then it is
necessary to use xsl:for-each
E.g. when the “selection” condition is very complex
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:for-each
2139
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr>
<xsl:for-each select="catalog/cd">
<tr> <td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td> </tr>
</xsl:for-each>
</table>
</body> </html>
</xsl:template>
A Semantic Web Primer, 2nd Edition
XML input document
<catalog>
<cd>
</cd>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
2140
…
</catalog>
A Semantic Web Primer, 2nd Edition
HTML output document
2141
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:for-each (alternative solution)
2142
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32"><th>Title</th><th>Artist</th></tr>
<xsl:apply-templates select="catalog/cd"/>
</table>
</body> </html>
</xsl:template>
<xsl:template match="cd">
<tr><td><xsl:value-of select="title"/></td>
<td> <xsl:value-of select="artist"/></td>
</tr>
</xsl:template>
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:sort


Used to sort the output
Where to put the Sort Information
–
–
2143
Inside an <xsl:for-each> element
Inside an <xsl:apply-templates> element
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:sort
2144
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr>
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<tr> <td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td> </tr>
</xsl:for-each>
</table>
</body> </html>
</xsl:template>
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:sort (alternative solution)
2145
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32"><th>Title</th><th>Artist</th></tr>
<xsl:apply-templates select="catalog/cd">
<xsl:sort select="artist"/>
</xsl:apply-templates>
</table>
</body> </html>
</xsl:template>
<xsl:template match="cd">
<tr><td><xsl:value-of select="title"/></td>
<td> <xsl:value-of select="artist"/> </td>
</tr>
A Semantic Web Primer, 2nd Edition
</xsl:template>
HTML output document
Sorted
2146
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:if

To put a conditional test against the content of the XML
–
–
Usually for optional element / attributes in the XML document
Output something only when the element is present
Syntax
<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>
 Where to Put the <xsl:if> Element

–
2147
–
E.g. inside an <xsl:for-each> element
E.g. inside an <xsl:template> element
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:if
2148
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr>
<xsl:for-each select="catalog/cd">
<xsl:if test="price &gt; 10">
<tr> <td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td> </tr>
</xsl:if>
</xsl:for-each>
</table>
</body> </html>
</xsl:template>
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:if (alternative solution - 1)
2149
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32"><th>Title</th><th>Artist</th></tr>
<xsl:apply-templates select="catalog/cd"/>
</table>
</body> </html>
</xsl:template>
<xsl:template match="cd">
<xsl:if test="price &gt; 10">
<tr><td><xsl:value-of select="title"/></td>
<td> <xsl:value-of select="artist"/> </td> </tr>
</xsl:if>
</xsl:template>
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:if (alternative solution - 2)
2150
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32"><th>Title</th><th>Artist</th></tr>
<xsl:apply-templates select="catalog/cd[price > 10]"/>
</table>
</body> </html>
</xsl:template>
<xsl:template match="cd">
<tr><td><xsl:value-of select="title"/></td>
<td> <xsl:value-of select="artist"/></td>
</tr>
</xsl:template>
A Semantic Web Primer, 2nd Edition
HTML output document
Filtered
2151
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:choose

Expresses multiple conditional tests
–
Used in conjunction with <xsl:when>, <xsl:otherwise>
Syntax
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
…
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>

2152
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:choose
2153
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th><th>Artist</th>
</tr>
<xsl:apply-templates select="catalog/cd"/>
</table>
</body>
</html>
</xsl:template>
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:choose
2154
<xsl:template match="cd">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price &gt; 10">
<td bgcolor="#ff00ff"><xsl:value-of select="artist"/> </td>
</xsl:when>
<xsl:when test="price &gt; 9">
<td bgcolor="#cccccc"><xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:template>
A Semantic Web Primer, 2nd Edition
HTML output document
Colored
2155
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:variable




2156
Used to declare a local or global variable.
The variable is global if it's declared as a toplevel element, and local if it's declared within a
template.
Once you have set a variable's value, you
cannot change or modify that value!
You can add a value to a variable by the
content of the <xsl:variable> element OR by
the select attribute!
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:variable - Syntax
<xsl:variable name="name“ select="expression">
<!-- Content:template -->
</xsl:variable>

Attributes
–
–
2157
name: Required. Specifies the name of the variable
select: Optional expression. Defines the value of the
variable
A Semantic Web Primer, 2nd Edition
XML input document
xsl:variable example (1)
2158
<catalog>
<cd catid="cd0001"> <title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd catid="cd0028"> <title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
…
</catalog>
A Semantic Web Primer, 2nd Edition
XML input document
xsl:variable example (2)
<order orderNo="23456"
customer="John Smith"
date="October 15, 2002">
<item itemNo="cd0001" productID="cd0001"
quantity="1"/>
<item itemNo="cd0028" productID="cd0028"
quantity="3"/>
2- </order>
159
A Semantic Web Primer, 2nd Edition
XML input document
xsl:variable example (3)
2160
<eshop>
<catalog>
<cd catid="…"> … </cd>
…
</catalog>
<orders>
<order> … </order>
…
</orders>
</eshop>
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:variable - example

We want to generate a list of orders, where
each list will contain
–
–
–

2161
ID and Date of the order
name of the client
List of items with their name (not id), quantity and
item price
Later we could add
–
–
total price per ordered items
Total price of the order
A Semantic Web Primer, 2nd Edition
xsl:variable – example
HTML Output
2162
A Semantic Web Primer, 2nd Edition
xsl:variable – example (1)
2163
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Order list</title>
</head>
<body>
<h1>List of Orders</h1>
<xsl:apply-templates select="eshop/orders/order"/>
</body>
</html>
</xsl:template>
A Semantic Web Primer, 2nd Edition
xsl:variable – example (2)
2164
<xsl:template match="order">
<h2>Order No. <xsl:value-of select="@orderNo"/> of
<xsl:value-of select="@customer"/> </h2>
<h3>Date: <xsl:value-of select="@date"/> </h3>
<p>Items:</p>
<table border="1">
<tbody>
<tr>
<th>Title</th> <th>Price per item</th> <th>Quantity</th>
</tr>
<xsl:apply-templates select="item"/>
</tbody>
</table>
</xsl:template>
A Semantic Web Primer, 2nd Edition
xsl:variable – example (3)
2165
<xsl:template match="item">
<xsl:variable name="product" select="@productID"/>
<tr>
<td>
<xsl:value-of select="//cd[@catid=$product]/title"/>
</td>
<td>
<xsl:value-of select="//cd[@catid=$product]/price"/>
</td>
<td><xsl:value-of select="@quantity"/></td>
</tr>
</xsl:template>
A Semantic Web Primer, 2nd Edition
xsl:variable – example (extended)
HTML Output
2166
A Semantic Web Primer, 2nd Edition
xsl:variable – extended example (2’)
2167
<xsl:template match="order">
<h2>Order No. <xsl:value-of select="@orderNo"/> of
<xsl:value-of select="@customer"/> </h2>
<h3>Date: <xsl:value-of select="@date"/> </h3>
<p>Items:</p>
<table border="1">
<tbody>
<tr>
<th>Title</th> <th>Price per item</th> <th>Quantity</th>
<th>Total price per item</th>
</tr>
<xsl:apply-templates select="item"/>
<tr><th>TOTAL</th> <th/>
<th><xsl:value-of select="sum(item/@quantity)"/></th><th/>
</tr>
</tbody>
</table>
</xsl:template>
A Semantic Web Primer, 2nd Edition
xsl:variable – extended example (3’)
2168
<xsl:template match="item">
<xsl:variable name="product" select="@productID"/>
<tr>
<td><xsl:value-of select="//cd[@catid=$product]/title"/></td>
<td><xsl:value-of select="//cd[@catid=$product]/price"/>
</td>
<td><xsl:value-of select="@quantity"/></td>
<td>
<xsl:value-of select="//cd[@catid=$product]/price * @quantity"/>
</td>
</tr>
</xsl:template>
A Semantic Web Primer, 2nd Edition
More XSLT constructs
xsl:variable – extended example



It is extremely difficult to
calculate the total price
for the order
It requires calling templates recursively
with parameters
You can find a similar case here:
–
2169
http://stackoverflow.com/questions/1333558/xslt-tosum-product-of-two-attributes
A Semantic Web Primer, 2nd Edition
More details on XSLT & XPath






2170
XSLT Tutorial: http://www.w3schools.com/xsl/
XPath Tutorial:
http://www.w3schools.com/xpath/
XSLT 2.0: http://www.w3.org/TR/xslt20/
XSLT 1.0: http://www.w3.org/TR/xslt
XPath 2.0: http://www.w3.org/TR/xpath20/
XPath 1.0: http://www.w3.org/TR/xpath/
A Semantic Web Primer, 2nd Edition
XML tools





Altova XML Spy (http://www.altova.com/xml-editor/)
<oXygen/> (http://www.oxygenxml.com/)
EditiX (http://www.editix.com/)
Liquid XML Studio (http://www.liquidtechnologies.com/)
Free editors
–
–
2171
–
XML Copy Editor (http://xml-copy-editor.sourceforge.net/)
EditiX Lite (http://free.editix.com/)
XML Notepad 2007
(http://www.microsoft.com/download/en/details.aspx?id=7
973)
A Semantic Web Primer, 2nd Edition
XML Summary

Metalanguage that allows users to define markup
–
–


Separates content and structure from formatting
De facto standard for the representation and
exchange of structured information on the Web
–
2172

Nesting of tags introduces structure.
The structure of documents can be enforced using
schemas or DTDs.
XML supports machine processing of information.
Supported by query languages
A Semantic Web Primer, 2nd Edition
Points for Discussion in Subsequent
Chapters




2173
Tag nesting does not have standard meaning
Semantics of XML documents is not accessible
to machines, only to people
Collaboration-exchange supported if there is
shared understanding of the vocabulary
XML is well-suited for close collaboration, where
community-based vocabularies are used
–
It is not so well-suited for global communication.
A Semantic Web Primer, 2nd Edition
Chapter 2
Structured Web Documents in XML
Grigoris Antoniou
Frank van Harmelen