+ All Categories
Home > Documents > Διδάσκων: Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής...

Διδάσκων: Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής...

Date post: 24-Feb-2016
Category:
Upload: eddy
View: 46 times
Download: 0 times
Share this document with a friend
Description:
ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ , ΑΠΘ ΜΕΤΑΠΤΥΧΙΑΚΟ ΠΡΟΓΡΑΜΜΑ ΣΠΟΥΔΩΝ Κατεύθυνση Πληροφοριακών Συστημάτων - 1ο Εξάμηνο Σημασιολογικός Ιστός lpis .csd.auth.gr/mtpx/sw/index.htm. Διδάσκων: Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ. Μαθήματα: 2-3 -4. Chapter 2 Structured Web Documents in XML. - PowerPoint PPT Presentation
183
ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ, ΑΠΘ ΜΕΤΑΠΤΥΧΙΑΚΟ ΠΡΟΓΡΑΜΜΑ ΣΠΟΥΔΩΝ Κατεύθυνση Πληροφοριακών Συστημάτων - 1ο Εξάμηνο Σημασιολογικός Ιστός lpis.csd.auth.gr/mtpx/sw/index.htm Διδάσκων: Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ Μαθήματα: 2-3-4
Transcript
Page 1: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ, ΑΠΘ ΜΕΤΑΠΤΥΧΙΑΚΟ ΠΡΟΓΡΑΜΜΑ ΣΠΟΥΔΩΝ

Κατεύθυνση Πληροφοριακών Συστημάτων - 1ο Εξάμηνο

Σημασιολογικός Ιστόςlpis.csd.auth.gr/mtpx/sw/index.htm

Διδάσκων: Ν. ΒασιλειάδηςΑναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

Μαθήματα: 2-3-4

Page 2: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

Chapter 2Structured Web Documents in XML

Grigoris AntoniouFrank van Harmelen

Page 3: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-3

Introduction

Today HTML (HyperText Markup Language) is the standard language for Web pages.

HTML was derived from SGML (Standard Generalized Markup Language)

SGML Applications– HTML: Developed because SGML is too complex for

Internet-related purposes. – XML (eXtensible Markup Language): Its development

was driven by shortcomings of HTML.

Page 4: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-4

An HTML Example

<h2>Nonmonotonic Reasoning: Context-Dependent Reasoning</h2>

<i>by <b>V. Marek</b> and <b>M. Truszczynski</b></i><br>

Springer 1993<br>ISBN 0387976892

Page 5: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-5

The Same Example in XML

<book><title>Nonmonotonic Reasoning: Context-

Dependent Reasoning</title><author>V. Marek</author><author>M. Truszczynski</author><publisher>Springer</publisher><year>1993</year><ISBN>0387976892</ISBN>

</book>

Page 6: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-6

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?

Page 7: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-7

HTML versus XML: Differences

All tags in XML must be closed – 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 In HTML some tags (<br>), may be left open. XHTML brings HTML more in line with XML

– Opening and closing tags in XHTML are balanced– Any valid XHTML document is also a valid XML

document

Page 8: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-8

Problems with Automated Interpretation of HTML Documents

An intelligent agent trying to retrieve the namesof 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”?

Page 9: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-9

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:– 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.

Page 10: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-10

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– E.g. a year must be a number of 4 digits

Page 11: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-11

HTML vs XML: Formatting

The HTML representation provides more than the XML representation: – The formatting of the document is also described

Τhe main use of an HTML document is to display information: it must define formatting

XML: separation of content from display– Same information can be displayed in different ways – Content may be used for purposes other than display

Page 12: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-12

HTML vs XML: Another Example

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>

Page 13: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-13

HTML vs XML: Different Use of Tags

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

Page 14: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-14

XML Vocabularies

Web applications must agree on common vocabularies to communicate and collaborate

Communities and business sectors are defining their specialized vocabularies– mathematics (MathML)– bioinformatics (BSML)– human resources (HRML) – …

Page 15: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-15

Data Exchange

The main use of XML today is as a uniform data exchange format between applications. – Rather than a document markup language.

Companies retrieve information from their partners, and update their databases accordingly.– 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.

Page 16: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-16

Lecture Outline

1. Introduction2. Detailed Description of XML3. Structuring

a) DTDsb) XML Schema

4. Namespaces5. Accessing, querying XML documents: XPath6. Transformations: XSLT

Page 17: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-17

The XML Language

An XML document consists of a prolog a number of elements an optional epilog (not discussed)

Page 18: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-18

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">

Page 19: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-19

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>

Page 20: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-20

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 – E.g. “Xml”, “xML”

Page 21: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-21

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/> for <lecturer></lecturer>

Page 22: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-22

XML Attributes

An empty element is not necessarily meaningless– Its presence alone may “mean” something– 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"/>

Page 23: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-23

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>

Page 24: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-24

The Same Example without Attributes

<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>

Page 25: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-25

XML Elements vs Attributes

Attributes can be replaced by elementsWhen to use elements and when

attributes is a matter of tasteBut attributes

– Cannot be nested – Cannot have two attributes with the same name in

the same element

Page 26: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

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-07-13</shipdate>.

</letter>

A Semantic Web Primer, 2nd Edition2-26

Page 27: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

Mixed Content

An element has also mixed content if it has attributes and text

<lecturer phone="+61 − 7 − 3875 507">David Billington

</lecturer>

A Semantic Web Primer, 2nd Edition2-27

Page 28: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-28

Further Components of XML Docs

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"?>

Page 29: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-29

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>– Attributes within an element have unique names– Element and tag names must be permissible

Page 30: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-30

The Tree Model of XML Documents: An Example

<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>

Page 31: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

Root

email

head

from

name

Michael Maher

address

[email protected]

to

name

Grigoris Antoniou

address

[email protected]

subject

Where is your draft?

body

Grigoris, where is the draft of the

paper you promised me last week?

A Tree Example

A Semantic Web Primer, 2nd Edition2-31

Page 32: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-32

The Tree Model of XML Docs

The tree representation of an XML document is an ordered labeled tree:– There is exactly one root– There are no cycles– Each non-root node has exactly one parent– Each node has a label.

Page 33: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-33

The Tree Model of XML Docs (2)

Element nodes are different from attribute nodes– 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

There is difference between:– the root of the tree (representing the XML document)– the root element (email)– This distinction is important for addressing and querying

XML documents.

Page 34: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-34

Lecture Outline

1. Introduction2. Detailed Description of XML3. Structuring

a) DTDsb) XML Schema

4. Namespaces5. Accessing, querying XML documents: XPath6. Transformations: XSLT

Page 35: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-35

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– They wish to use the same vocabulary

Page 36: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-36

Structuring XML Documents

Define all the element and attribute names that may be used

Define the structure – 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

Page 37: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-37

Structuring XML Documents (2)

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)

Page 38: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-38

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)>

Page 39: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-39

The Meaning of the DTD

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

Page 40: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-40

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)>

Both elements below are validated:<lecturer>

<name>David Billington</name></lecturer><lecturer>

<phone> +61 − 7 − 3875 507 </phone></lecturer>

A lecturer with both name and phone is not validated!

Page 41: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-41

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>

Page 42: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-42

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>

Page 43: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-43

The Corresponding DTD

<!ELEMENT order (item+)><!ATTLIST order

orderNo ID #REQUIREDcustomer CDATA #REQUIREDdate CDATA #REQUIRED>

<!ELEMENT item EMPTY><!ATTLIST item

itemNo ID #REQUIREDquantity CDATA #REQUIREDcomments CDATA #IMPLIED>

Page 44: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-44

Comments on the DTD

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

Page 45: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-45

Comments on the DTD (2)

In addition to defining elements, we define attributes

This is done in an attribute list containing:– 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

Page 46: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-46

DTD: Attribute Types

Similar to predefined data types, but limited selection The most important types are

– 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.

Page 47: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-47

DTD: Attribute Value Types

#REQUIRED– Attribute must appear in every occurrence of the

element type in the XML document #IMPLIED

– The appearance of the attribute is optional "value"

– This specifies the default value for the attribute #FIXED "value"

– Every element must have this attribute

Page 48: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-48

Referencing with IDREF and IDREFS

<!ELEMENT family (person*)><!ELEMENT person (name)><!ELEMENT name (#PCDATA)><!ATTLIST person id ID #REQUIRED

mother IDREF #IMPLIED father IDREF #IMPLIED children IDREFS #IMPLIED>

Page 49: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-49

An XML Document Respecting the DTD

<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>

Page 50: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-50

The email example

<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>

Page 51: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

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-51 A Semantic Web Primer, 2nd Edition

Page 52: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-52

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>

Page 53: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-53

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>

Page 54: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-54

Interesting Parts of the DTD

A head element contains (in that order):– a from element– at least one to element– zero or more cc elements– a subject element

In from, to, and cc elements – the name attribute is not required– the address attribute is always required

Page 55: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-55

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”– “mime” is the default value

Page 56: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-56

Remarks on DTDs

A DTD can be interpreted as an Extended Backus-Naur 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>

Page 57: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-57

DTD for the “original” Email example

<!ELEMENT email (head, body)>…<!ELEMENT body (#PCDATA)>

Page 58: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

DTD for Mixed Content

<letter>  Dear Mr.<name>John Smith</name>.  Your order <orderid>1032</orderid>  will be shipped on <shipdate>2001-07-13</shipdate>.

</letter><!ELEMENT letter

(#PCDATA | name | orderid | shipdate)*><!ELEMENT cname (#PCDATA)><!ELEMENT orderid (#PCDATA)><!ELEMENT shipdate (#PCDATA)>

A Semantic Web Primer, 2nd Edition2-58

Page 59: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

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-59 A Semantic Web Primer, 2nd Edition

Page 60: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-60

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>

Page 61: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-61

Lecture Outline

1. Introduction2. Detailed Description of XML3. Structuring

a) DTDsb) XML Schema

4. Namespaces5. Accessing, querying XML documents: XPath6. Transformations: XSLT

Page 62: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-62

XML Schema

Significantly richer language for defining the structure of XML documents

Its syntax is based on XML itself– not necessary to write separate tools (e.g. parser)

Reuse and refinement of schemas– Expand or restrict already existent data types

Sophisticated set of data types– compared to DTDs (which only supports strings)

Page 63: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-63

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

– Definitions of element and attribute types using data types

Page 64: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-64

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)

– Generalizations of *,?,+ offered by DTDs

Page 65: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-65

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

Default value: default="x" Fixed value: fixed="x"

Incompatible

Page 66: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-66

Data Types

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

Page 67: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-67

Data Types (2)

Complex data types are defined from already existing data types by defining some attributes (if any), and using:– 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

Page 68: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-68

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"/>

</complexType><element name=“lecturer"

type="lecturerType"/>

Page 69: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-69

Data Type Extension 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></complexType>

Page 70: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-70

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>

Page 71: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-71

Data Type Extension (2)

A hierarchical relationship exists between the original and the extended type– 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

Page 72: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-72

Data Type Restriction

An existing data type may be restricted by adding constraints on certain values

New type and use attributes may be added minOccurs, maxOccurs may be tightened

Page 73: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-73

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></complexType>

Page 74: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-74

Data Type Restriction

Restriction is not the opposite from extension – Elements or attributes cannot be deleted

Hierarchical relationship: – Instances of restricted type are also instances of

original type – They satisfy at least the constraints of original

type

Page 75: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-75

Restriction of Simple Data Types

<simpleType name="dayOfMonth"><restriction base="integer">

<minInclusive value="1"/><maxInclusive value="31"/>

</restriction></simpleType>

Page 76: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-76

Data Type Restriction: Enumeration

<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>

Page 77: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

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

Page 78: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-78

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>

Page 79: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-79

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>

Page 80: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-80

XML Schema: The Email Example (3)

<complexType name="nameAddress"><attribute name="name"

type="string" use="optional"/><attribute name="address"

type="string" use="required"/>

</complexType>Notice how an empty complex type is defined in XML Schema

Page 81: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-81

XML Schema: The Email Example (4)

<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"/>…

Page 82: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-82

Remarks for the e-Mail Example

Some data types are defined separately and given names

Other data types are defined (anonymously) within other types – E.g. the types for the attachment element and the

encoding attribute If a type is only used once, it makes sense to

define it anonymously for local use.– However, this approach reaches its limitations

quickly if nesting becomes too deep.

Page 83: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-83

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>

<complexType name=“attachmentType”><attribute name="encoding" type=“encodingType” use=“optional" default ="mime“/><attribute name="file" type="string" use="required"/>

</complexType>

Page 84: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-84

XML Schema: The Email Example (4b)

<simpleType name=“encodingType”><restriction base="string">

<enumeration value="mime"/><enumeration value="binhex"/>

</restriction></simpleType>

Page 85: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

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>

2-85

<letter>  Dear Mr.<name>John Smith</name>.  Your order <orderid>1032</orderid>  will be shipped on <shipdate>2001-07-13</shipdate>.

</letter> A Semantic Web Primer, 2nd Edition

Page 86: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

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-86 A Semantic Web Primer, 2nd Edition

Page 87: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-87

XML Schema for Email with mixed content

<complexType name="bodyType"><sequence><element name="text" type="string"/><element name="attachment" type=“attachmentType”minOccurs="0" maxOccurs="unbounded“/><sequence>

</complexType>

<complexType name="bodyType" mixed="true"><sequence><element name="attachment" type=“attachmentType”minOccurs="0" maxOccurs="unbounded"/><sequence>

</complexType>

Old

New

Page 88: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-88

Lecture Outline

1. Introduction2. Detailed Description of XML3. Structuring

a) DTDsb) XML Schema

4. Namespaces5. Accessing, querying XML documents: XPath6. Transformations: XSLT

Page 89: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-89

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 – prefix:name

Page 90: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-90

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>

Page 91: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-91

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:– 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

Page 92: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-92

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"/>

<academicStaff title="lecturer“ name="Mate Jones" school="Information Technology"/>

</vu:instructors>

Page 93: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-93

Lecture Outline

1. Introduction2. Detailed Description of XML3. Structuring

a) DTDsb) XML Schema

4. Namespaces5. Accessing, querying XML documents: XPath6. Transformations: XSLT

Page 94: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-94

Addressing and Querying XML Documents

In relational databases, parts of a database can be selected and retrieved using SQL– Same necessary for XML documents– Query languages: XQuery, XQL, XML-QL

The central concept of XML query languages is a path expression – Specifies how a node or a set of nodes, in the tree

representation of the XML document can be reached

Page 95: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-95

XPath

XPath is core for XML query languages Language for addressing parts of an XML

document. – It operates on the tree data model of XML– It has a non-XML syntax

XPath expressions can be used for purposes other than querying– For transforming XML documents.

Page 96: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-96

Types of Path Expressions

Absolute (starting at the root of the tree)– 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

Page 97: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-97

An XML Example

<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>

Page 98: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

root

library

location

Bremen

author

name

Henry Wise

book

title

Artificial Intelligence

book

title

Modern Web

Services

book

title

Theory of Computation

author

name

William Smart

book

title

Artificial Intelligen

ce

author

name

Cynthia Singleton

book

title

The Semantic

Web

book

title

Browser Technolo

gy Revised

Tree Representation

2-98

Page 99: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition2-99

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

Page 100: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-100

Examples of Path Expressions in XPath (2)

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

Page 101: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-101

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

Page 102: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-102

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"

Page 103: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

root

library

location

Bremen

author

name

Henry Wise

book

title

Artificial Intelligence

book

title

Modern Web

Services

book

title

Theory of Computation

author

name

William Smart

book

title

Artificial Intelligen

ce

author

name

Cynthia Singleton

book

title

The Semantic

Web

book

title

Browser Technolo

gy Revised

Tree Representation of Query 4

2-103

Non-empty set; return true

Page 104: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-104

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

– It restricts the set of addressed nodes. Difference with query 4.

– 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

Page 105: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

root

library

location

Bremen

author

name

Henry Wise

book

title

Artificial Intelligence

book

title

Modern Web

Services

book

title

Theory of Computation

author

name

William Smart

book

title

Artificial Intelligen

ce

author

name

Cynthia Singleton

book

title

The Semantic

Web

book

title

Browser Technolo

gy Revised

Tree Representation of Query 5

A Semantic Web Primer, 2nd Edition

2-105

Page 106: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-106

Examples of Path Expressions in XPath (6)

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)]

Page 107: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XPath

How can we address the first book of each author?

//author/book[1] The following will work as well

//book[1]

2-107 A Semantic Web Primer, 2nd Edition

Page 108: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-108

General Form of Path Expressions

A path expression consists of a series of steps, separated by slashes

A step consists of – An axis specifier, – A node test, and – An optional predicate

Page 109: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-109

General Form of Path Expressions (2)

An axis specifier determines the tree relationship between the nodes to be addressed and the context node– E.g. parent, ancestor, child (the default),

sibling, attribute node– // is such an axis specifier: descendant or

self

Page 110: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-110

General Form of Path Expressions (3)

A node test specifies which nodes to address – The most common node tests are element

names E.g. //author/book E.g., * addresses all element nodes

– comment() addresses all comment nodes

Page 111: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-111

General Form of Path Expressions (4)

Predicates (or filter expressions) are optional and are used to refine the set of addressed nodes– 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

Page 112: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-112

More XPath Examples (1)

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

Page 113: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-113

More XPath Examples (2)

/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

Page 114: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-114

More XPath Examples (3)

. 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

Page 115: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

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 – Could be written also as:

para[position()=5 and @type="warning"] 2-115 A Semantic Web Primer, 2nd Edition

Page 116: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XPath Examples (5)

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

2-116 A Semantic Web Primer, 2nd Edition

Page 117: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-117

Lecture Outline

1. Introduction2. Detailed Description of XML3. Structuring

a) DTDsb) XML Schema

4. Namespaces5. Accessing, querying XML documents: XPath6. Transformations: XSLT

Page 118: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-118

Displaying XML Documents

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.

Page 119: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-119

Displaying XML Documents

<author><name>Grigoris Antoniou</name><affiliation>University of Bremen</affiliation><email>[email protected]</email>

</author>may be displayed in different ways:

Grigoris Antoniou Grigoris AntoniouUniversity of Bremen University of [email protected] [email protected]

Page 120: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-120

Style Sheets

Style sheets can be written in various languages– E.g. CSS2 (cascading style sheets level 2)– XSL (extensible stylesheet language)

XSL includes – a transformation language (XSLT)– a formatting language– Both are XML applications

Page 121: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-121

XSL Transformations (XSLT)

XSLT specifies rules with which an input XML document is transformed to– 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

Page 122: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-122

XSLT (2)

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

Page 123: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

Process of XSL Transformations

2-123

Altova XML Spy

Any modern browser

Page 124: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-124

XSLT Transformation into HTML

<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>

Page 125: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-125

Style Sheet Output

<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>

Page 126: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-126

Observations About XSLT

XSLT documents are XML documents – XSLT resides on top of XML

The XSLT document defines a template– 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

Page 127: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-127

A Template

<html><head><title>An author</title></head><body bgcolor="white">

<b>...</b><br>...<br><i>...</i>

</body></html>

Page 128: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-128

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

Page 129: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-129

Example of an Auxiliary Template

<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>

Page 130: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-130

Desired Output

Page 131: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-131

Example of an Auxiliary Template (2)

<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>

Page 132: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-132

Example of an Auxiliary Template (3)

<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>

Page 133: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-133

Multiple Authors Output

<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>

Page 134: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-134

Explanation of the Example

xsl:apply-templates element causes all children of the context node to be matched against the selected path expression– 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

Page 135: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-135

Explanation of the Example (2)

It is good practice to define a template for each element type in the document– 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

Page 136: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

Alternative XSLT

<xsl:template match="/"><html><head><title>Authors</title></head><body bgcolor="white"><xsl:apply-templates select=“//author"/></body></html>

</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> Lectures 2 & 3: Information Sharing

on the Web: XML

2-136

Page 137: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-137

Processing XML Attributes

Suppose we wish to transform the following element:

<person firstname="John" lastname="Woo"/>– to

<person name="John Woo"/>

Page 138: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

Processing XML Attributes Wrong solution

<xsl:template match="person"> <person name="<xsl:value-of select="@firstname"> <xsl:value-of select="@lastname">"/></xsl:template>

– Not well-formed because tags are not allowed within the values of attributes

– XSL commands are ignored because they are included in double quotes2-

138 A Semantic Web Primer, 2nd Edition

Page 139: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

Processing XML AttributesCorrect Solution

We wish to add attribute values into template

<xsl:template match="person"> <person name="{@firstname} {@lastname}"/></xsl:template>

2-139 A Semantic Web Primer, 2nd Edition

Page 140: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

Transforming an XML Document to Another

Root

authors

author

name

Grigoris Antonio

u

affiliation

University of

Bremen

email

[email protected]

author

name

David Billingto

n

affiliation

Griffith Universi

ty

email

[email protected].

net

2-140

Root

authors

author

name

Grigoris Antonio

u

contact

institute

University of

Bremen

email

[email protected]

author

name

David Billingto

n

contact

institute

Griffith Universi

ty

email

[email protected].

net

Page 141: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-141

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"/></author>

</xsl:template>

Page 142: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-142

Transforming an XML Document to Another (3)

<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>

Page 143: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl: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– 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

2-143

Page 144: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:for-each

<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

2-144

Page 145: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

XML input document

<catalog><cd> <title>Empire Burlesque</title>

<artist>Bob Dylan</artist><country>USA</country><company>Columbia</company><price>10.90</price><year>1985</year>

</cd><cd> <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

2-145

Page 146: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

HTML output document

A Semantic Web Primer, 2nd Edition

2-146

Page 147: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:for-each (alternative solution)<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

2-147

Page 148: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:sort Used to sort the output Where to put the Sort Information

– Inside an <xsl:for-each> element– Inside an <xsl:apply-templates> element

A Semantic Web Primer, 2nd Edition

2-148

Page 149: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:sort<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

2-149

Page 150: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:sort (alternative solution)<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></xsl:template> A Semantic Web Primer, 2nd Edition

2-150

Page 151: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

HTML output document

A Semantic Web Primer, 2nd Edition

2-151

Sorted

Page 152: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl: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

– E.g. inside an <xsl:for-each> element– E.g. inside an <xsl:template> element

A Semantic Web Primer, 2nd Edition

2-152

Page 153: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:if<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

2-153

Page 154: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:if (alternative solution - 1)

<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

2-154

Page 155: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:if (alternative solution - 2)

<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

2-155

Page 156: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

HTML output document

A Semantic Web Primer, 2nd Edition

2-156

Filtered

Page 157: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl: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>

A Semantic Web Primer, 2nd Edition

2-157

Page 158: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:choose<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

2-158

Page 159: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:choose<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

2-159

Page 160: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

HTML output document

A Semantic Web Primer, 2nd Edition

2-160

Colored

Page 161: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:variable

Used to declare a local or global variable. The variable is global if it's declared as a top-

level 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

2-161

Page 162: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:variable - Syntax

<xsl:variable name="name“ select="expression"> <!-- Content:template --></xsl:variable>

Attributes– name: Required. Specifies the name of the variable– select: Optional expression. Defines the value of the

variable

A Semantic Web Primer, 2nd Edition

2-162

Page 163: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

XML input documentxsl:variable example (1)<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

2-163

Page 164: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-164

XML input documentxsl:variable example (2)

<order orderNo=“o23456" customer="John Smith" date="October 15, 2002"><item itemNo="i123" productID="cd0001" quantity="1"/><item itemNo="i124" productID="cd0028" quantity="3"/>

</order>

Page 165: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

XML input documentxsl:variable example (3)

<eshop><catalog><cd catid="…"> … </cd>…</catalog><orders><order> … </order>…</orders>

</eshop>A Semantic Web Primer, 2nd Edition

2-165

Page 166: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:variable - example

We want to generate a list of orders, where each list will contain – 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

2-166

Page 167: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:variable – exampleHTML Output

2-167

Page 168: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:variable – example (1)

<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

2-168

Page 169: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:variable – example (2)

<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

2-169

Page 170: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:variable – example (3)

<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

2-170

Page 171: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:variable – example (extended)HTML Output

A Semantic Web Primer, 2nd Edition

2-171

Page 172: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:variable – extended example (2’)

<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

2-172

Page 173: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:variable – extended example (3’)

<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

2-173

Page 174: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

More XSLT constructsxsl:variable – extended example

It is extremely difficult to calculate the total price for the order

A Semantic Web Primer, 2nd Edition

2-174

It requires calling templates recursively with parameters

… but it is doable!

Page 175: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:variable/param – example (extended++)HTML Output

2-175

Page 176: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:param – extended example++ (2’’)

<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> <xsl:call-template name="sum"><xsl:with-param name="nodes" select="item" /></xsl:call-template></th></tr></tbody> </table>

</xsl:template>

2-176 Lectures 2 & 3: Information Sharing on the Web: XML

Page 177: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:param – extended example (4)

<xsl:template name="sum"> <xsl:param name="nodes" /><xsl:param name="sum" select="0" /><xsl:variable name="curr" select="$nodes[1]" /><xsl:variable name="product" select="$curr/@productID"/><xsl:if test="$curr"> <!-- if we have a node, calculate & recurse --> <xsl:variable name="runningsum" select="$sum + //cd[@catid=$product]/price * $curr/@quantity" /> <xsl:call-template name="sum"><xsl:with-param name="nodes" select="$nodes[position() &gt; 1]" /><xsl:with-param name="sum" select="$runningsum" /> </xsl:call-template></xsl:if><xsl:if test="not($curr)"> <!-- if we don't have a node (last step), return sum --><xsl:value-of select="$sum" /></xsl:if>

</xsl:template>

2-177 Lectures 2 & 3: Information Sharing on the Web: XML

Page 178: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

xsl:param

Similar to xsl:variable, but… The value specified on the xsl:param variable

is only a default value for the binding When the template or stylesheet within which

the xsl:param element occurs is invoked, parameters may be passed that are used in place of the default values.

A Semantic Web Primer, 2nd Edition

2-178

Page 179: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

More details on XSLT & XPath

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/

2-179

Page 180: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

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.liquid-technologies.com/) Free editors

– 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=7973)2-180

Page 181: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-181

XML Summary

Metalanguage that allows users to define markup – Nesting of tags introduces structure. – The structure of documents can be enforced using

schemas or DTDs. Separates content and structure from formatting De facto standard for the representation and

exchange of structured information on the Web – XML supports machine processing of information.

Supported by query languages

Page 182: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

A Semantic Web Primer, 2nd Edition

2-182

Points for Discussion in Subsequent Chapters

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.

Page 183: Διδάσκων:  Ν. Βασιλειάδης Αναπλ. Καθ. Τμ. Πληροφορικής ΑΠΘ

Chapter 2Structured Web Documents in XML

Grigoris AntoniouFrank van Harmelen


Recommended