• Aucun résultat trouvé

XSLT Web Data Management and Distribution Serge Abiteboul Philippe Rigaux Marie-Christine Rousset Pierre Senellart

N/A
N/A
Protected

Academic year: 2022

Partager "XSLT Web Data Management and Distribution Serge Abiteboul Philippe Rigaux Marie-Christine Rousset Pierre Senellart"

Copied!
28
0
0

Texte intégral

(1)

XSLT

Web Data Management and Distribution

Serge Abiteboul Philippe Rigaux Marie-Christine Rousset Pierre Senellart

http://gemo.futurs.inria.fr/wdmd

January 15, 2010

(2)

Basics

What is XSLT?

XSLT = a specialized language for transforming an XML document into another XML document.

Main principles:

An XSLT program, or stylesheet, consists of rules, or templates.

A template applies to a specific kind of node of the input document, and produces a fragment of the output document.

by creatingliteral nodes,

bycopying valuesandfragmentsfrom the input document,

byinstantiating(= calling) other templates.

Execution model: initially, a template is applied to theroot node of the input document

this first template may initiate a traversal of the input document.

Remark

An XSLT stylesheet is an XML document! XSLT element names are prefixed by xsl:, the XSLT namespace.

(3)

Basics

A Hello World! Stylesheet

<x s l : s t y l e s h e e t

x m l n s : x s l=" http: // www . w3 . org /1999/ XSL / T r a n s f o r m"

v e r s i o n=" 1.0 ">

<x s l : o u t p u t method =" xml " e n c o d i n g=" utf-8 " / >

<x s l : t e m p l a t e match =" / ">

< hello > world </ hello >

</x s l : t e m p l a t e>

</x s l : s t y l e s h e e t>

General structure of a stylesheet:

A top-level <xsl:stylesheet> element, with a version="1.0"

attribute.

Some declarations (all elements except <xsl:template> ones), in this case just a <xsl:output> one.

Some template rules, in this case a template that applies to the root node.

(4)

Basics

Invocation of an XSLT Stylesheet

An XSLT stylesheet may be invoked:

Programmatically, through one of the various XSLT libraries.

Through a command lineinterface.

In a Web Publishing context, by including a styling processing instruction in the XML document

<?x m l - s t y l e s h e e t

href =" toto . xsl" type =" text / xsl " ? >

< doc >

< titi / >

</ doc >

the transformation can be processed on theserver sideby a CGI, PHP, ASP, JSP. . . script

or on theclient sidethrough the XSLT engines integrated to most browsers.

(5)

Basics

Web Publishing with XSLT

HTML

Wap client HTML

XML document

Network

Network

Web client

Web client Web server

Web server XML document

HTML WML

XSLT stylesheet

XSLT stylesheet XSLT

stylesheet stylesheet

XSLT

(6)

Templates

The

<xsl:template>

Element

<x s l : t e m p l a t e match =" book ">

The book title is:

<x s l : v a l u e - o f select =" title " / >

< h2 > A u t h o r s list </ h2 >

< ul >

<x s l : a p p l y - t e m p l a t e s select =" a u t h o r s/ name " / >

</ ul >

</x s l : t e m p l a t e>

A template consists of

A pattern an XPath expression (restricted) which determines the node to which the template applies.

The pattern is the value of the match attribute.

A body an XML fragment (well-formed!) which is inserted in the output document when the template is instantiated.

(7)

Templates

XPath patterns in XSLT

The role of the XPath expression of the match attribute is quite specific: it describes the nodes which can be the target of a template instantiation.

Those expressions are called patterns. They must comply to the following requirements

A pattern always denotes a node set.

Example: <xsl:template match=’1’> is incorrect.

It must be easy to decide whether a node is denoted or not by a pattern.

Example: <xsl:template match=’preceding::*[12]’> is meaningful, but quite difficult to evaluate.

Patterns syntax

A pattern is a valid XPath expression which uses only thechildand @axes, and the abbreviation //. Predicates are allowed.

(8)

Templates

Pattern examples

Recall: a pattern is interpreted as the nodes to which a template applies.

<xsl:template match=’B’>

applies to any B element.

<xsl:template match=’A/B’>

applies to any B element, child of anA element.

<xsl:template match=’@att1’>

applies to any att1attribute, whatever its parent element.

<xsl:template match=’A//@att1’>

applies to any att1attribute, if its parent element is a descendant of an A element.

General rule

Given an XML treeT, a patternP matchesa node N if there exists a node C (thecontext node) in T such thatN P(T,C).

(9)

Templates

Content of a template body

Basically, the content of <xsl:template> may consist of:

Literal elements and text.

Example: <h2>Authors</h2>. This creates in the output document an element h2, with aTextchild node ’Authors’.

Values and elements from the input document.

Example: <xsl:value-of select=’title’/>. This inserts in the output document a node set, result of the XPath expression title. Call to other templates.

Example: <xsl:apply-templates select=’authors’/>. Applies a template to each node in the node set result of the XPath expression authors.

Remark

Only the basic of XSLT programming! Many advanced features (modes, priorities, loops and tests) beyond this core description.

(10)

Templates

Instantiation of a

<xsl:template>

Main principles:

Literal elements (those that don’t belong to the XSLT namespace) and text are simply copied to the output document.

Context node: A template is always instantiated in the context of a node from the input document.

XPath expressions: all the (relative) XPath expression found in the template are evaluated with respect to the context node (an exception: <xsl:for-each>).

Calls with xsl:apply-templates: find and instantiate a template for each node selected by the XPath expression select.

Template call substitution: any call to other templates is eventually replaced by the instantiation of these templates.

(11)

Applying templates

The

xsl:apply-templates

element

<x s l : a p p l y - t e m p l a t e s

select =" a u t h o r s/ name "

mode =" main "

/ >

select an XPath expression which, if relative, is interpreted with respect to the context node,

Note: the default value ischild::node(), i.e., select all the children of the context node

mode a label which can be used to specify which kind of template is required.

(12)

Applying templates

The

xsl:apply-templates

mechanism

<x s l : t e m p l a t e match =" book ">

< ul > <x s l : a p p l y - t e m p l a t e s

select =" a u t h o r s/ name " / > </ ul >

</x s l : t e m p l a t e>

<x s l : t e m p l a t e match =" name ">

< li > <x s l : v a l u e - o f select =" . " / > </ li >

</x s l : t e m p l a t e>

< book >

...

< a u t h o r s >

< name > Serge </ name >

< name > Ioana </ name >

</ a u t h o r s >

</ book >

−→

< ul >

< li > Serge </ li >

< li > Ioana </ li >

</ ul >

(13)

Applying templates

Combined templates instantiation

book

title

Web [...]

authors

name

Serge name

Ioanna

content

chapter

XML DM chapter

XPath

<x s l : t e m p l a t e match =" book ">

Title: <x s l : v a l u e - o f

select =" title " / >

< h2 > A u t h o r s </ h2 >

< ul > <x s l : a p p l y - t e m p l a t e s select =" a u t h o r s/ name " / >

</ ul >

</x s l : t e m p l a t e>

Title: Web [...]

< h2 > A u t h o r s </ h2 >

< ul >

< li > Serge </ li >

< li > Ioanna </ li >

</ ul >

(14)

Applying templates

The execution model of XSLT

An XSLT stylesheet consists of a set of templates. The transformation of an input documentI proceeds as follows:

1 The engine considers theroot node R ofI, and selects the template that applies to R.

2 The template body is copied in the output documentO.

3 Next, the engine considers all the <xsl:apply-templates> that have been copied inO, and evaluate theselectXPath expression, taking R as context node.

4 For each node result of the XPath evaluation, a template is selected, and its body replaces the <xsl:apply-templates> call.

5 The process iterates, as new <xsl:apply-templates> may have been inserted in O.

6 The transform terminates when O is free ofxsl: instructions.

(15)

Applying templates

The execution model: illustration

match found

The templates list

A B

C C

B /

Target node

...

The output tree The input tree

/

match_1 body_1 match_2 body_2 match_n body_n

(16)

Applying templates

The execution model: illustration

Instantiate template 2 The templates list

A B

C C

B

Context node

<xsl:apply−templates select="A/B">

/ body_2

match_1 body_1 match_2 ... match_n body_n

The output tree The input tree

body_2 /

(17)

Applying templates

The execution model: illustration

Instantiate template 2 The templates list

A

C C

<xsl:apply−templates select="A/B">

Context node

B B

/

Evaluation of A/B body_1

/

match_2 ... match_n body_n

The output tree The input tree

body_2

body_2 match_1

(18)

Applying templates

The execution model: illustration

The templates list

A

C C

<xsl:apply−templates select="A/B">

B B

Target node

/ Match found

match_1 body_1 match_2 ... match_n body_n

The output tree The input tree

/ body_2

body_2

(19)

Applying templates

The execution model: illustration

The templates list

A

C C

B

Instantiate template 1

Context node

B

body_1

match_1 match_2 ... match_n body_n

The output tree The input tree

/ body_2

body_2 /

body_1

(20)

Applying templates

The execution model: illustration

The templates list

A

C C

B Target node

Instantiate template 1

B

body_1

match_1 match_2 ... match_n body_n

The output tree The input tree

/ body_2

body_2 /

body_1

body_1

(21)

A full example

The input document in serialized and tree forms

<? xml v e r s i o n=" 1.0 "

e n c o d i n g=" utf-8 "? >

< book >

< title > Web [...] </ title >

< a u t h o r s >

< name > Serge </ name >

< name > Ioanna </ name >

</ a u t h o r s >

< c o n t e n t >

< c h a p t e r id =" 1 ">

XML data model

</ c h a p t e r >

< c h a p t e r id =" 2 ">

XPath

</ c h a p t e r >

</ c o n t e n t >

</ book >

Document

book

title

Web [...]

authors

name

Serge name

Ioanna

content

chapter

XML DM chapter

XPath

(22)

A full example

The XSLT template that matches the root node

<x s l : t e m p l a t e match =" / ">

< html >

< head >

< title >

<x s l : v a l u e - o f select =" / book / title "/ >

</ title >

</ head >

< body b g c o l o r=" white ">

<x s l : a p p l y - t e m p l a t e s select =" book "/ >

</ body >

</ html >

</x s l : t e m p l a t e>

Remark

Typical of Web publishing templates.

(23)

A full example

The output document after instantiating the template

Document

html

head

title

xsl:value-of Context node: /

select book/title

body

xsl:apply-templates Context node: /

select book

(24)

A full example

The output document after evaluation of

<xsl:value-of>

Document

html

head

title

Web [...]

body

xsl:apply-templates Context node: /

select book

(25)

A full example

The remaining templates

<x s l : t e m p l a t e match =" book ">

The book title is:

<x s l : v a l u e - o f select =" title " / >

< h2 > A u t h o r s list </ h2 >

< ul >

<x s l : a p p l y - t e m p l a t e s select =" a u t h o r s/ name " / >

</ ul >

</x s l : t e m p l a t e>

<x s l : t e m p l a t e match =" a u t h o r s/ name "/ >

< li > <x s l : v a l u e - o f select =" . "/ > </ li >

</x s l : t e m p l a t e>

(26)

A full example

The

<book>

element is selected the book template is instantiated

Document

html

head

title

Web [...]

body

The book title is:

xsl:value-of Context: <book>

select title

h2

Authors list

ul

xsl:apply-templates Context: <book>

select authors/name

(27)

A full example

The authors/name template is instantiated twice, one for each author

Document

html

head

title

Web [...]

body

The book title is:

xsl:value-of Context: book

select title

h2

Authors list

ul

li

xsl:value-of Context: authors/name[1]

select .

li

xsl:value-of Context: authors/name[2]

select .

(28)

A full example

The final output document

Document

html

head

title

Web [...]

body

The book title is: Web [...] h2

Authors list

ul

li

Serge li

Ioanna

Références

Documents relatifs

Single keyword query: just consult the index and return the documents in index order. Boolean

The serialized form is a textual, linear representation of the tree; it complies to a (sometimes complicated) syntax;.. There exist an object-oriented model for the tree form:

It provides a hierarchical representation, where each node is an object instance of a DOM class.. Normalized by the W3C

The expression “textual value of an Element N” denotes the concatenation of all the Text node values which are descendant of N, taken in the document order.. Gemo, Lamsade, LIG,

Multiple document input, and multiple document output document function, &lt;xsl:document&gt; element (XSLT 2.0, but widely implemented in XSLT 1.0 as an extension

a native XML atomic type, which can be queried in XQuery style a set of XML publishing functions: extracting XML elements out of relational data by querying. mapping rules:

insert, delete, replace, rename: updating expressions transform: non-updating expression.. XQuery

Single keyword query: just consult the index and return the documents in index order. Boolean