38
Introduction to XSLT

Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Introduction to XSLT

Page 2: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

What is XSL

• XSL stands for eXtensible Stylesheet Language

• XSLT transforms an XML document into another XML document

• XSL is a XML language

Page 3: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

What is XSL

• XSLT - a language for transforming XML documents

• XPath - a language for navigating in XML documents

• XSL-FO - a language for formatting XML documents

Page 4: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

What is XSL

• XSLT - a language for transforming XML documents

• XPath - a language for navigating in XML documents

• XSL-FO - a language for formatting XML documents

Page 5: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Why XSL

• CSS adds styles (font, color) to HTML elements

• XSL = XML Style Sheets – allows you to transform an XML file into an HTML file or another text-based format

Page 6: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Why two style sheet languages?

CSS XSL

Can be used with HTML? yes no

Can be used with XML? yes yes

Transformation language? no yes

Syntax CSS XML

Page 7: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

XSLT

Page 8: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Why XSLT?• XML is not a fixed tag set (like HTML) and has no (application) semantics

• XML markup does not (usually) include formatting information

• Reuse: the same content can look different in different contexts

• Multiple output formats: different media (paper, online), different sizes (manuals, reports), different classes of output devices (workstations, hand-held devices)

• Styles tailored to the reader's preference (e.g., accessibility): print size, color, simplified layout for audio readers

• Standardized styles: corporate stylesheets can be applied to the content at any time

• Freedom from style issues for content authors: technical writers needn't be concerned with layout issues because the correct style can be applied later

Page 9: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

The basic idea of XSLT

XML document(”source tree”)

XSLT stylesheet

XSLT processor Web page

Page 10: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

The basic idea of XSLT

XML document(”source tree”)

XSLT stylesheet

XSLT processor Web page

Separate informationfrom presentation!!!

Page 11: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

The basic idea of XSLT

XSLT stylesheet

XML document(”source tree”)

XSLT processor Web page

Server Browser

Page 12: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

The basic idea of XSLT

XSLT stylesheet

XML document(”source tree”)

XSLT processorWeb page

Server Browser

Page 13: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Simple XSLT transformation

<source><employee> <firstName>Joe</firstName> <surname>Smith</surname>

</employee> </source>

Page 14: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

source

employee

firstName surname

Joe Smith

<xsl:template match="/"><html> <body> <h1>My employee</h1> <xsl:value-of select="employee/firstName"/> <xsl:value-of select="employee/surname"/> </body></html></xsl:template>

Simple XSLT transformation

Page 15: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

source

employee

firstName surname

Joe Smith

<xsl:template match="/"><html> <body> <h1>My employee</h1> <xsl:value-of select="employee/firstName"/> <xsl:value-of select="employee/surname"/> </body></html></xsl:template>

<html> <body> <h1>My employee</h1> <xsl:value-of select="employee/firstName"/> <xsl:value-of select="employee/firstName"/> </body></html>

Simple XSLT transformation

Page 16: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

<xsl:template match="/"><html> <body> <h1>My employee</h1> <xsl:value-of select="employee/firstName"/> <xsl:value-of select="employee/surname"/> </body></html></xsl:template>

source

employee

firstName surname

Joe Smith

<html> <body> <h1>My employee</h1> <xsl:value-of select="employee/firstName"/> <xsl:value-of select="employee/firstName"/> </body></html>

Simple XSLT transformation

Page 17: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

<xsl:template match="/"><html> <body> <h1>My employee</h1> <xsl:value-of select="employee/firstName"/> <xsl:value-of select="employee/surname"/> </body></html></xsl:template>

source

employee

firstName surname

Joe Smith

<html> <body> <h1>My employee</h1> Joe <xsl:value-of select="employee/firstName"/> </body></html>

Simple XSLT transformation

Page 18: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

<xsl:template match="/"><html> <body> <h1>My employee</h1> <xsl:value-of select="employee/firstName"/> <xsl:value-of select="employee/surname"/> </body></html></xsl:template>

source

employee

firstName surname

Joe Smith

<html> <body> <h1>My employee</h1> Joe <xsl:value-of select="employee/firstName"/> </body></html>

Simple XSLT transformation

Page 19: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

<xsl:template match="/"><html> <body> <h1>My employee</h1> <xsl:value-of select="employee/firstName"/> <xsl:value-of select="employee/surname"/> </body></html></xsl:template>

source

employee

firstName surname

Joe Smith

<html> <body> <h1>My employee</h1> Joe Smith </body></html>

Simple XSLT transformation

Page 20: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Result<html> <body> <h1>My employee</h1> Joe Smith </body></html>

Page 21: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

source

employee

firstName surname

Joe Smith

<xsl:template match="/"><html> <body> <h1>My employee</h1> <xsl:value-of select="employee/firstName"/> <xsl:value-of select="employee/surname"/> </body></html></xsl:template>

Simple XSLT transformation

employee/firstName is a XPath expression

Page 22: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

source

employee

firstName surname

Joe Smith

<xsl:template match="/"><html> <body> <h1>My employee</h1> <xsl:apply-templates select=”employee/ firstName”/> <xsl:apply-templates select=”employee/ surname”/> </body></html></xsl:template>

<xsl:template match=”firstName”><b><xsl:value-of select=”.”/></b>

</xsl:template>

<xsl:template match=”surname”><i><xsl:value-of select=”.”/></i>

</xsl:template>

Simple XSLT transformation

Page 23: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

source

employee

firstName surname

Joe Smith

<xsl:template match="/"><html> <body> <h1>My employee</h1> <xsl:apply-templates select=”employee/ firstName”/> <xsl:apply-templates select=”employee/ surname”/> </body></html></xsl:template>

<html> <body> <h1>My employee</h1> <xsl:apply-templates select=”employee/ firstName”/> <xsl:apply-templates select=”employee/ surname”/> </body></html>

Simple XSLT transformation

Page 24: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

source

employee

firstName surname

Joe Smith

<xsl:template match=”firstName”><b><xsl:value-of select=”.”/></b>

</xsl:template>

<html> <body> <h1>My employee</h1> <xsl:apply-templates select=”employee/ firstName”/> <xsl:apply-templates select=”employee/ surname”/> </body></html>

Simple XSLT transformation

Page 25: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

source

employee

firstName surname

Joe Smith

<xsl:template match=”firstName”><b><xsl:value-of select=”.”/></b>

</xsl:template>

<html> <body> <h1>My employee</h1> <b>Joe</b> <xsl:apply-templates select=”employee/ surname”/> </body></html>

Simple XSLT transformation

Page 26: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

<xsl:template match=”surname”><i><xsl:value-of select=”.”/></i>

</xsl:template>

<html> <body> <h1>My employee</h1> <b>Joe</b> <i>Smith</i> </body></html>

source

employee

firstName surname

Joe Smith

Simple XSLT transformation

Page 27: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Result<html> <body> <h1>My employee</h1> <b>Joe</b> <i>Smith</i> </body></html>

Page 28: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

The <xsl:for-each> Element<table> <xsl:for-each select=”employee”> <tr> <td> <xsl:value-of select=”firstName”/> </td> <td> <xsl:value-of select=”surname”/> </td> </tr> </xsl:for-each></table>

Joe Smith

Eric Gibson

Ada Hook

Page 29: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

The <xsl:sort> Element<table> <xsl:for-each select=”employee”> <xsl:sort select="firstName"/> <tr> <td> <xsl:value-of select=”firstName”/> </td> <td> <xsl:value-of select=”surname”/> </td> </tr> </xsl:for-each></table>

Ada Hook

Joe Smith

Eric Gibson

Page 30: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

The <xsl:if> Element<table> <xsl:for-each select=”employee”> <xsl:if test="surname=‘Smith’"> <tr> <td> <xsl:value-of select=”firstName”/> </td> <td> <xsl:value-of select=”surname”/> </td> </tr> </xsl:for-each></table>

Joe Smith

Page 31: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

XPath

Page 32: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

XPath<?xml version="1.0" encoding="ISO-8859-1"?><bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book></bookstore>

Page 33: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Selecting nodesExpression Description

nodename Selects all child nodes of the node

/ Selects from the root node

//Selects nodes in the document from thecurrent node that match the selection no matter where they are

. Selects the current node

.. Selects the parent of the current node

@ Selects attributes

Page 34: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Selecting nodes

<?xml version="1.0" encoding="ISO-8859-1"?><bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book></bookstore>

/bookstore/bookSelects all attributes that are named lang

Page 35: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Selecting nodes

<?xml version="1.0" encoding="ISO-8859-1"?><bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book></bookstore>

//@langSelects all attributes that are named lang

Page 36: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Predicates

<?xml version="1.0" encoding="ISO-8859-1"?><bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book></bookstore>

/bookstore/book[1]Selects the first book element that is the child of the bookstore element

Page 37: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

Predicates

<?xml version="1.0" encoding="ISO-8859-1"?><bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book></bookstore>

/bookstore/book[price>35.00]

Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00

Page 38: Introduction to XSLT€¦ · Why XSLT? • XML is not a fixed tag set (like HTML) and has no (application) semantics • XML markup does not (usually) include formatting information

References

• W3C

• http://www.w3.org/Style/XSL/

• Tutorials

• http://www.w3schools.com/xsl/

• http://www.topxml.com/xsl/tutorials/intro/default.asp