33
XSLT (part I) Mario Alviano University of Calabria, Italy A.Y. 2017/2018 1 / 22

XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

XSLT(part I)

Mario Alviano

University of Calabria, Italy

A.Y. 2017/2018

1 / 22

Page 2: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Outline

1 Introduction

2 Templates

3 Attributes

4 Copy of elements

5 Exercises

2 / 22

Page 3: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Outline

1 Introduction

2 Templates

3 Attributes

4 Copy of elements

5 Exercises

3 / 22

Page 4: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

What is XSLT?

XSLT is a (Turing complete) functional language

It used for transforming XML documents to other XMLdocuments (or text document in general)Often used to transform XML data to web pagesModern web browsers implement XSLT

To link an XSLT stylesheet to an XML document use aprocessing instruction<?xml-stylesheet href="file_name.xsl" type="text/xsl" ?>

4 / 22

Page 5: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

What is XSLT?

XSLT is a (Turing complete) functional languageIt used for transforming XML documents to other XMLdocuments (or text document in general)Often used to transform XML data to web pages

Modern web browsers implement XSLTTo link an XSLT stylesheet to an XML document use aprocessing instruction<?xml-stylesheet href="file_name.xsl" type="text/xsl" ?>

4 / 22

Page 6: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

What is XSLT?

XSLT is a (Turing complete) functional languageIt used for transforming XML documents to other XMLdocuments (or text document in general)Often used to transform XML data to web pagesModern web browsers implement XSLT

To link an XSLT stylesheet to an XML document use aprocessing instruction<?xml-stylesheet href="file_name.xsl" type="text/xsl" ?>

4 / 22

Page 7: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Output format

XSLT stylesheets are XML filesXSLT is an XML application

Root element xsl:stylesheet (or xsl:transform)<?xml version="1.0" encoding="UTF-8" ?><xsl:stylesheet

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="2.0">...

</xsl:stylesheet>

By default, XSLT produces in output an XML fileDifferent formats can be obtained by specifying<xsl:output><xsl:output method="html" encoding="UTF-8" indent="yes" />

5 / 22

Page 8: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Outline

1 Introduction

2 Templates

3 Attributes

4 Copy of elements

5 Exercises

6 / 22

Page 9: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Templates

XSLT works with templates which are applied on elementsselected by XPath expressions

Templates are defined by <xsl:template>Attribute match specifies an XPath expression to selectnodes of the XML document in inputThe content defines what to produce in output for anymatched node

The simplest output is literal

Example. Literal output

<xsl:template match="/"><html>

<body>Hello, World!!!

</body></html>

</xsl:template>

7 / 22

Page 10: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Templates

XSLT works with templates which are applied on elementsselected by XPath expressionsTemplates are defined by <xsl:template>

Attribute match specifies an XPath expression to selectnodes of the XML document in inputThe content defines what to produce in output for anymatched node

The simplest output is literal

Example. Literal output

<xsl:template match="/"><html>

<body>Hello, World!!!

</body></html>

</xsl:template>

7 / 22

Page 11: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Templates

XSLT works with templates which are applied on elementsselected by XPath expressionsTemplates are defined by <xsl:template>

Attribute match specifies an XPath expression to selectnodes of the XML document in inputThe content defines what to produce in output for anymatched node

The simplest output is literal

Example. Literal output

<xsl:template match="/"><html>

<body>Hello, World!!!

</body></html>

</xsl:template>

7 / 22

Page 12: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Read input

It is possible to write in output data read from inputUse the element <xsl:value-of>

Attribute select specifies what to select in the input file(an XPath expression!)

Example. Use of value-of

<xsl:template match="/"><html>

<head><title>

<xsl:value-of select="reference/body/title" /></title>

</head><body>

Hello, World!!!</body>

</html></xsl:template>

8 / 22

Page 13: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Apply other templates

In the content of a template it is possible to specify theapplication of other templatesUse the element <xsl:apply-templates>

Attribute select specifies what to select (again, an XPathexpression!)

Example. Use of apply-templates

<body><xsl:apply-templates select="reference/body" />

</body>

For each element in the sequence the most appropriatetemplate will be usedIf no template matches a selected node, XSLT produces inoutput the textual value of the node and callsapply-templates to all childrenNote: if select is omitted then all children of the currentnode are selected

9 / 22

Page 14: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Apply other templates

In the content of a template it is possible to specify theapplication of other templatesUse the element <xsl:apply-templates>

Attribute select specifies what to select (again, an XPathexpression!)

Example. Use of apply-templates

<body><xsl:apply-templates select="reference/body" />

</body>

For each element in the sequence the most appropriatetemplate will be used

If no template matches a selected node, XSLT produces inoutput the textual value of the node and callsapply-templates to all childrenNote: if select is omitted then all children of the currentnode are selected

9 / 22

Page 15: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Apply other templates

In the content of a template it is possible to specify theapplication of other templatesUse the element <xsl:apply-templates>

Attribute select specifies what to select (again, an XPathexpression!)

Example. Use of apply-templates

<body><xsl:apply-templates select="reference/body" />

</body>

For each element in the sequence the most appropriatetemplate will be usedIf no template matches a selected node, XSLT produces inoutput the textual value of the node and callsapply-templates to all children

Note: if select is omitted then all children of the currentnode are selected

9 / 22

Page 16: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Apply other templates

In the content of a template it is possible to specify theapplication of other templatesUse the element <xsl:apply-templates>

Attribute select specifies what to select (again, an XPathexpression!)

Example. Use of apply-templates

<body><xsl:apply-templates select="reference/body" />

</body>

For each element in the sequence the most appropriatetemplate will be usedIf no template matches a selected node, XSLT produces inoutput the textual value of the node and callsapply-templates to all childrenNote: if select is omitted then all children of the currentnode are selected

9 / 22

Page 17: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Outline

1 Introduction

2 Templates

3 Attributes

4 Copy of elements

5 Exercises

10 / 22

Page 18: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Attributes

Use <xsl:attribute> to add an attribute to an elementThe name of the attribute is specified in nameThe value of the attribute is specified in the content

<p><xsl:attribute name="align">

right</xsl:attribute>...

</p>is equivalent to<p align="right">

...</p>

11 / 22

Page 19: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Groups of attributes

To create a group of attributes (to be used later)<xsl:attribute-set name="row">

<xsl:attribute name="border">1</xsl:attribute></xsl:attribute-set>

Attribute xsl:use-attribute-sets can be used tospecify an attribute-set to be added to an element<td xsl:use-attribute-sets="row">

12 / 22

Page 20: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Groups of attributes

To create a group of attributes (to be used later)<xsl:attribute-set name="row">

<xsl:attribute name="border">1</xsl:attribute></xsl:attribute-set>

Attribute xsl:use-attribute-sets can be used tospecify an attribute-set to be added to an element<td xsl:use-attribute-sets="row">

12 / 22

Page 21: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Outline

1 Introduction

2 Templates

3 Attributes

4 Copy of elements

5 Exercises

13 / 22

Page 22: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Elements copy

<xsl:copy> allows to copy a node in output<xsl:template match="code">

<xsl:copy><xsl:apply-templates />

</xsl:copy></xsl:template>

Warning! <xsl:copy> only produces in output openingand closing tags of the selected nodeFor this reason we used <xsl:apply-templates />inside <xsl:copy>

An alternative is <xsl:copy-of><xsl:template match="code">

<xsl:copy-of select="." /></xsl:template>

14 / 22

Page 23: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Elements copy

<xsl:copy> allows to copy a node in output<xsl:template match="code">

<xsl:copy><xsl:apply-templates />

</xsl:copy></xsl:template>

Warning! <xsl:copy> only produces in output openingand closing tags of the selected nodeFor this reason we used <xsl:apply-templates />inside <xsl:copy>

An alternative is <xsl:copy-of><xsl:template match="code">

<xsl:copy-of select="." /></xsl:template>

14 / 22

Page 24: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Elements copy

<xsl:copy> allows to copy a node in output<xsl:template match="code">

<xsl:copy><xsl:apply-templates />

</xsl:copy></xsl:template>

Warning! <xsl:copy> only produces in output openingand closing tags of the selected nodeFor this reason we used <xsl:apply-templates />inside <xsl:copy>

An alternative is <xsl:copy-of><xsl:template match="code">

<xsl:copy-of select="." /></xsl:template>

14 / 22

Page 25: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Outline

1 Introduction

2 Templates

3 Attributes

4 Copy of elements

5 Exercises

15 / 22

Page 26: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

How to apply XSLT stylesheet

XSLT with libxmlxsltproc XSLTfile XMLfile

XSLT with Eclipse EESelect XML and XSLT filesRight-click, then Run as | XSL Transformation

16 / 22

Page 27: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

How to apply XSLT stylesheet

XSLT with libxmlxsltproc XSLTfile XMLfile

XSLT with Eclipse EESelect XML and XSLT filesRight-click, then Run as | XSL Transformation

16 / 22

Page 28: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Exercises (1)

Exercise 1Write an XML document with content<hello>World</hello>

Write an XSLT stylesheet transforming the XML documentin an HTML file with title “Hello ” followed by the content ofthe tag <hello>

Moreover, the XSLT must add in the body of the HTML atag <h1> with the same content of the element titleApply the transformationLink the XSLT to the XML document, then open the XMLdocument with a browser

17 / 22

Page 29: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Exercises (2)

Exercise 2Retrieve file library.xmlWrite an XSLT transforming element and attribute namesfrom English to ItalianNote: use <xsl:value-of> to obtain attribute valuesHint: define a group of attributes with<xsl:attribute-set>

18 / 22

Page 30: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Exercises (3)

Exercise 3Write an XSLT transforming the document library.xml tothe HTML page shown below

19 / 22

Page 31: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Exercises (4)

Exercise 4Write an XSLT transforming the document library.xml tothe HTML page shown below

20 / 22

Page 32: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

Exercises (5)

Exercise 5Write an XSLT transforming the document library.xml tothe HTML page shown below

21 / 22

Page 33: XSLT (part I)alviano/archives/teaching/krr... · XSLT works with templates which are applied on elements selected by XPath expressions Templates are defined by

END OF THELECTURE

22 / 22