Top Banner
XSL: eXtensible Style Language 1 Anabel Fraga
38

XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Jul 31, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XSL: eXtensible Style Language

1

Anabel Fraga

Page 2: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Table of Contents

� Presenting with XML

� XSL

2

� XSLT

� Learning by Example

� Elements

� XSL-FO

� References

Page 3: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Presenting with XML

� Presentation in HTML is done basically within the browsers.

� It would be interesting programming the presentation (code reuse)

3

presentation (code reuse)

� The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language.

� So Stylesheets emerged:– CSS: Cascading Style Sheets for HTML

– XSL: eXtensible Style Language for XML (XML + [DTD or XML Schema] + XSL Style file)

Page 4: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XSL (I)

� XSL is a family of recommendations for defining XML documents and consists of 3 parts:

� XSL Transformations (XSLT)

4

� XSL Transformations (XSLT) – An XML language for transforming XML documents

� XML Path Language (XPath) – Is an expression language for addressing parts of an XML

document, or for computing values

� XSL Formatting Objects (XSL-FO) – An XML language for specifying the visual formatting of an

XML document

Page 5: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XSL (II)

5

Page 6: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XSL (III)

CSS XSL

� Can be used with HTML? Yes No

6

� Can be used with XML? Yes Yes

� Has/Uses Transformations? No Yes

� Syntax used CSS XML

Page 7: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XSLT

� XSLT is the most important part of XSL.

Used for transforming an XML document into

another XML, HTML, WML, etc. document.

7

another XML, HTML, WML, etc. document.

� XSLT can add new elements or even

eliminate. Can even do tests or take

decisions.

� Normally we refer to an XML source tree

transformed to an XML result tree.

Page 8: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Elements

� Template rules

– xsl:template

– xsl:apply-templates

– xsl:call-template

8

– xsl:call-template

� Output

– xsl:value-of

– xsl:copy-of

– xsl:output

� Flow control

– xsl:for-each

– xsl:if

– xsl:choose, xsl:when, xsl:otherwise

Page 9: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Example

<?xml version="1.0“ encoding="iso-8859-1"?>

<?xml-stylesheet type="text/xsl" href=“reference.xslt"?>

<source>

<title>XSL</title>

<html>

<head>

<title>This is a test</title></head>

<body>

<h1>XSL</h1>

XSL

John Smith

9

<title>XSL</title>

<author>John Smith</author>

</source>

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

<xsl:template match="/">

<html>

<head><title>This is a test</title></head>

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

<h2><xsl:value-of select="source/author"/></h2> </body>

</html>

</xsl:template>

</xsl:stylesheet>

<h1>XSL</h1>

<h2>John Smith</h2>

</body>

</html>

Page 10: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (I)

Consider the following XML :

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="cdcatalog.xslt"?>

<catalog>

10

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

.

.

</catalog>

Page 11: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (II) [ xsl:value-of ]

And its linked XSTL file :

<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

11

<xsl:template match="/">

<html>

<body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32"><th>Title</th><th>Artist</th></tr>

<tr>

<td><xsl:value-of select="catalog/cd/title"/></td>

<td><xsl:value-of select="catalog/cd/artist"/></td>

</tr>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Page 12: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (III) [ xsl:value-of ]

12

Page 13: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (IV) [ xsl:for-each ]

And its linked XSTL file :

<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">

13

<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="catalog/cd/title"/></td>

<td><xsl:value-of select="catalog/cd/artist"/></td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Page 14: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (V) [ xsl:for-each ]

14

Page 15: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (VI) [ xsl:sort ]

And its linked XSTL file :

<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">

15

<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="catalog/cd/title"/></td>

<td><xsl:value-of select="catalog/cd/artist"/></td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Page 16: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (VII) [ xsl:sort ]

16

Page 17: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (VIII) [ xsl:if ]

And its linked XSTL file :

<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">

17

<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="catalog/cd/title"/></td>

<td><xsl:value-of select="catalog/cd/artist"/></td>

</tr>

</xsl:if></xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Page 18: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (IX) [ xsl:if ]

18

Page 19: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (X) [ xsl:if ] Cont..

Some examples for “conditions” in XSLT

Consider the following XML:

19

<?xml version="1.0" encoding="UTF-8"?>

<poem author="jm" year="1667"><verse>Seest thou yon dreary Plain, forlorn and wild,</verse>

<verse>The seat of desolation, void of light,</verse>

<verse>Save what the glimmering of these livid flames</verse>

<verse>Casts pale and dreadful?</verse>

</poem>

Page 20: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (XI) [ xsl:if ] Cont..

<xsl:if test="@author='jm'">

1. The poem's author is jm.

</xsl:if>

<xsl:if test="@author">

2. The poem has an author attribute.

</xsl:if>

<xsl:if test="count(verse) > 3">

6. The poem has more than 3 verse child elements.

</xsl:if>

<xsl:if test="count(verse) < 3">

7. The poem has less than 3 verse child elements.

20

</xsl:if>

<xsl:if test="@flavor">

3. The poem has a flavor attribute.

</xsl:if>

<xsl:if test="verse">

4. The poem has at least one verse child element.

</xsl:if>

<xsl:if test="shipDate">

5. The poem has at least one shipDate child element.

</xsl:if>

7. The poem has less than 3 verse child elements.

</xsl:if>

<xsl:if test="(@author = 'bd') or (@year='1667')">

8. Either the author is "bd" or the year is "1667".

</xsl:if>

<xsl:if test="@year < '1850'">

9a. The poem is old.

<xsl:if test="@year < '1700'">

9b. The poem is very old.

</xsl:if>

<xsl:if test="@year < '1500'">

9c. The poem is very, very old.

</xsl:if>

</xsl:if>

Page 21: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (XII) [ xsl:choose ]

<xsl:template match="/">

<html><body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Artist</th>

21

<th>Artist</th>

</tr>

<xsl:for-each select="catalog/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:otherwise>

<td><xsl:value-of select="artist"/></td>

</xsl:otherwise>

</xsl:choose>

</tr>

</xsl:for-each>

</table>

</body></html>

</xsl:template>

} “xsl:when” can be repeated

Page 22: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (XIII) [ xsl:choose ]

22

Page 23: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (XIV) [xsl:apply-templates xsl:call-templates]

<?xml version="1.0" encoding="ISO-8859-1"?>

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">

<html>

<body>

<h2>My CD Collection</h2>

<xsl:call-template name=“total"/>

23

<xsl:call-template name=“total"/>

</body>

</html>

</xsl:template>

<xsl:template name=“total">

<xsl:apply-templates/>

</xsl:template>

<xsl:template match="cd">

<p>

<xsl:apply-templates select="title"/>

<xsl:apply-templates select="artist"/>

</p>

</xsl:template>

<xsl:template match="title">

<br/>Title: <span style="color:#ff0000"><xsl:value-of select="."/></span>

</xsl:template>

<xsl:template match="artist">

<br/>Artist: <span style="color:#00ff00"><xsl:value-of select="."/></span>

</xsl:template>

</xsl:stylesheet>

Page 24: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Learn by Example (XIII) [ xsl:choose ]

24

Page 25: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Template Rules – Things to Note

� xsl:template

� xsl:apply-templates

25

� xsl:call-template

Page 26: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

xsl:template (I) – Things to Note

Definition of a template rule

<xsl:template match=“pattern”>

body

26

body

</xsl:template>

“pattern” is :

– “/” matches whole document (matches root)

– “elementX” matches contents of element “elementX”

Page 27: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

xsl:apply-template – Things to Note

Application of the template rules

� <xsl:apply-templates/>

Processes all the child nodes

27

– Processes all the child nodes

� <xsl:apply-templates select=“expression”/>

– Only processes the nodes selected through

expression

Page 28: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

xsl:call-template (I) – Things to Note

� <xsl:template name=“name”/>

– names a rule

<xsl:call-template name=“name”/>

28

� <xsl:call-template name=“name”/>

– Calls a rule by its name

Page 29: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Exercise: Hello World! (I)

� Make an XML document and an XSLT that

presents “Hello World!” in HTML

29

Page 30: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Exercise: Hello World! (II)

<?xml version="1.0" encoding="iso-8859-1"?>

<?xml-stylesheet type="text/xsl" href="hola.xslt"?>

<greeting>Hello World!</greeting>

30

<greeting>Hello World!</greeting>

Page 31: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

Exercise: Hello World! (III)

<?xml version="1.0" encoding="iso-8859-1"?>

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

<xsl:template match="/">

<html>

31

<html>

<head><title>A Greeting</title></head>

<body>

<p><font color="red“ face=“arial”><strong>

<xsl:value-of select=“greeting"/>

</strong></font></p>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Page 32: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XSLT Processors

In order to apply an XSL stylesheet to an XML

document we can use:

– MSXML3 SP4 (Internet Explorer)

32

– MSXML3 SP4 (Internet Explorer)

http://www.microsoft.com/xml

– Saxon

http://users.iclway.co.uk/mhkay/saxon/

– Xalan

(Apache.org)

http://xml.apache.org/xalan/overview.html

– Search on Google for: XSLT validator

Page 33: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XML

Document

nokia.xsl

sony.xsl

ie5.xsl

ie4.xsl

nav3.xsl

33

Documentsony.xsl

edi_x.xsl

sap_y.xsl

flat_z.xsl

Page 34: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XML

Document

table.xsl

bars.xsl

34

Document

art.xsl

Page 35: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XSL-FO (Formatting Objects)

� XSL-FO stylesheet provides the specification

of an XML document for its subsequent

transformation and uses for this formatting

35

transformation and uses for this formatting

vocabulary.

� Permits the generation of high quality

printouts such as PDF or PS

� Useful for the production of documents

visually compound and elaborated

Page 36: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XSL – XSLT – XSL-FO

XML

DOCUMENT

XSL-FO

DOCUMENT

FINAL

DOCUMENT

36

XSLT

DOCUMENT

XSLT

PROCESSOR

XSL-FO

PROCESSOR

Page 37: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

XSL-FO Processors

� Antenna House XSL Formatter: An

interactive tool for XSL-FO

– http://www.AntennaHouse.com

37

– http://www.AntennaHouse.com

� Adobe Acrobat: A visualization tool for PDF

documents. Created by RenderX

– http://www.RenderX.com

Page 38: XSL: eXtensible Style Languageocw.uc3m.es/ingenieria-informatica/...notes-1/... · XSL (I) XSL is a family of recommendations for defining XML documents and consists of 3 parts: XSL

References

� http://www.w3.org/TR/1999/REC-xslt-19991116

� http://www.w3.org/TR/xsl/

� http://www.zvon.org/index.php?nav_id=tutorials

� http://www.bayes.co.uk/xml/index.xml

38

� http://www.bayes.co.uk/xml/index.xml

� http://www.xml-web.de

� http://www.xsl-rp.dexml.coverpages.org/xsl.html

� http://www.ibiblio.org/xml

� http://xml.apache.org/fop (processor)

� http://foa.sourceforge.net (editor)

� http://www.alphaworks.ibm.com/tech/xfc (editor y processor)

� Tecnet Consultores. XSLT Study by Juan Carlos Alonso.

� http://www.xml.com/pub/a/2002/03/20/xsl-fo.html?page=1