Chapter 5 - XSLT

50
Extensible Stylesheet Language transformation – (XSLt) CSC 570 – XML Programming

description

XML note c5

Transcript of Chapter 5 - XSLT

Extensible Stylesheet Language transformation – (XSLt)

CSC 570 – XML Programming

Chapter Overview

• Basic of XSL Transformation (XSLT)• Using XSLT Template and match • Using apply-templates• Using Select• Using Multiple Templates• Using Looping Statement• Filtering and Sorting XML Data• Using output method• XSLT Issues

XSLT

• XSLT stands for Extensible Stylesheet Language Transformations

• XSLT is used to transform XML documents into other kinds of documents--usually, but not necessarily, XHTML

• XSLT uses two input files:– The XML document containing the actual data– The XSL document containing both the “framework”

in which to insert the data, and XSLT commands to do so

How XSLT works

• The XML text document is read in and stored as a tree of nodes

• The template is used to select the entire tree

• The rules within the template are applied to the matching nodes, thus changing the structure of the XML tree– If there are other templates, they must be called explicitly from the

main template

• Unmatched parts of the XML tree are not changed

• After the template is applied, the tree is written out again as a text document

How XSLT works cont..

XSL Transformation

• A transformation can take place in one of three locations:– On the server– On the client (for example, your web

browser)– With a standalone program

XSL Transformation cont..

XSL Transformation cont..

• An xml document can be transform into

PDF

XML

Text

HTML

Where XSLT can be used

• With an appropriate program, XSLT can be used to read and write files

• A server can use XSLT to change XML files into HTML files before sending them to the client

• A modern browser can use XSLT to change XML into HTML on the client side

• Most users seldom update their browsers

– If you want “everyone” to see your pages, do any XSL processing on the server side

– Otherwise, think about what best fits your situation

Chapter Overview

• Basic of XSL Transformation (XSLT)• Using XSLT Template and match • Using apply-templates• Using Select• Using Multiple Templates• Using Looping Statement• Filtering and Sorting XML Data• Using output method• XSLT Issues

XSL Processors

• XSL processors do not:– Read your document in order and apply

templates– Read the templates in order and apply them

to the document

• XSL processors:– Look for the template(s) that match the root– Apply it (them) to the document

Style Sheet Declaration

• The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.

– Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used.

• The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:

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

or:

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

Style Sheet Declaration cont..

• To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.

• The xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

points to the official W3C XSLT namespace.

Style Sheet Declaration example

xsl:template

• The <xsl:template> element is used to build templates.

• Each template has two parts– a label that identifies the nodes in the document

that the template is applied to– instructions about the actual transformation

• Processor looks for the root template– which is applied to the root node of the

document

xsl:template example

xsl:template match

• Common match to root:– <xsl:template match=“/”>

• Another matches to root:– <xsl:template match=“/[transaction]” >– Matches a root with the name <transaction>

• The most specific match is checked first and applied, if possible.

xsl:template match cont..• Highest level match

<xsl:template match=“*|/”><xsl:apply-templates/>

</xsl:template>

– Applied if there is no other document root match defined

• Lowest level match

<xsl:template match=“text()|@*”><xsl:value-of select=“.”/>

</xsl:template>

– Applied to tag if nothing else matches it

match example

Chapter Overview

• Basic of XSL Transformation (XSLT)• Using XSLT Template and match • Using apply-templates• Using Select• Using Multiple Templates• Using Looping Statement• Filtering and Sorting XML Data• Using output method• XSLT Issues

• Used to call other templates

• Sets the context to the path defined in the XPath Expression

– If not specified, the current context node is used

• Remember the default

<xsl:template match=“*|/”><xsl:apply-templates/>

</xsl:template

xsl:apply-templates

xsl:value-of

• <xsl:value-of select="XPath expression"/> selects the contents of an element and adds it to the output stream– The select attribute is required– Notice that xsl:value-of is not a container,

hence it needs to end with a slash

• Example: <h1> <xsl:value-of

select="message"/> </h1>

Chapter Overview

• Basic of XSL Transformation (XSLT)• Using XSLT Template and match • Using apply-templates• Using Select• Using Multiple Templates• Using Looping Statement• Filtering and Sorting XML Data• Using output method• XSLT Issues

Value-of example

• File data.xml:

<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="render.xsl"?><message>Howdy!</message>

File render.xsl:

<?xml version="1.0"?><xsl:stylesheet version="1.0” xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- one rule, to transform the input root (/) --> <xsl:template match="/">

<html><body> <h1><xsl:value-of select="message"/></h1>

</body></html> </xsl:template></xsl:stylesheet>

Chapter Overview

• Basic of XSL Transformation (XSLT)• Using XSLT Template and match • Using apply-templates• Using Select• Using Multiple Templates• Using Looping Statement• Filtering and Sorting XML Data• Using output method• XSLT Issues

Multiple templates

• Modularization--breaking up a complex program into simpler parts--is an important programming tool– In programming languages modularization is

often done with functions or methods– In XSL we can do something similar with

xsl:apply-templates

• For example, suppose we have a DTD for book with parts titlePage, tableOfContents, chapter, and index– We can create separate templates for each of

these parts

Book example

• <xsl:template match="/"> <html> <body> <xsl:apply-templates/> </body> </html></xsl:template>

• <xsl:template match="tableOfContents"> <h1>Table of Contents</h1> <xsl:apply-templates select="chapterNumber"/> <xsl:apply-templates select="chapterName"/> <xsl:apply-templates select="pageNumber"/></xsl:template>

Applying templates to children

• <book> <title>XML</title> <author>Gregory Brill</author> </book>

• <xsl:template match="/"> <html> <head></head> <body> <b><xsl:value-of select="/book/title"/></b> <xsl:apply-templates select="/book/author"/> </body> </html></xsl:template>

<xsl:template match="/book/author"> by <i><xsl:value-of select="."/></i></xsl:template>

With this line:XML by Gregory Brill

Without this line:XML

End of part 1

• Your Test 1 will be on 26th April 2014 (Saturday), 9.30am – 11.30 am @ DK A.

• Assignment 1 and 2 is in my i-learn. Submit during lecture after midsem break. You can submit early to my room, 57.

• Quiz 2 – DTD will be on 23th April, during lecture.

Chapter Overview

• Basic of XSL Transformation (XSLT)• Using XSLT Template and match • Using apply-templates• Using Select• Using Multiple Templates• Using Looping Statement• Filtering and Sorting XML Data• Using output method• XSLT Issues

Using Looping Statement

• xsl:for-each is a kind of loop statement

• The syntax is <xsl:for-each select="XPath expression"> Text to insert and rules to apply </xsl:for-each>

• Example: to select every book (//book) and make an unordered list (<ul>) of their titles (title), use:

• <ul> <xsl:for-each select="//book"> <li> <xsl:value-of select="title"/> </li> </xsl:for-each> </ul>

Chapter Overview

• Basic of XSL Transformation (XSLT)• Using XSLT Template and match • Using apply-templates• Using Select• Using Multiple Templates• Using Looping Statement• Filtering and Sorting XML Data• Using output method• XSLT Issues

Filtering output

• You can filter (restrict) output by adding a criterion to the select attribute’s value:

<ul> <xsl:for-each select="//book"> <li> <xsl:value-of select="title[../author='Terry Pratchett']"/> </li> </xsl:for-each> </ul>

This will select book titles by Terry Pratchett

Filter details

• Here is the filter we just used:

<xsl:value-of select="title[../author='Terry Pratchett'"]/>

• author is a sibling of title, so from title we have to go up to its parent, book, then back down to author

• This filter requires a quote within a quote, so we need both single quotes and double quotes

• Legal filter operators are: = != &lt; &gt;

But it doesn’t work right!

• Here’s what we did:

<xsl:for-each select="//book"> <li> <xsl:value-of select="title[../author='Terry Pratchett']"/> </li> </xsl:for-each>

This will output <li> and </li> for every book, so we will get empty bullets for authors other than Terry Pratchett

There is no obvious way to solve this with just xsl:value-of

xsl:if

• xsl:if allows us to include content if a given condition (in the test attribute) is true

• Example: <xsl:for-each select="//book"> <xsl:if test="author='Terry Pratchett'"> <li> <xsl:value-of select="title"/> </li> </xsl:if> </xsl:for-each>

• This does work correctly!

xsl:choose

• The xsl:choose ... xsl:when ... xsl:otherwise construct is XML’s equivalent of Java’s switch ... case ... default statement

• The syntax is:<xsl:choose> <xsl:when test="some condition"> ... some code ... </xsl:when> <xsl:otherwise> ... some code ... </xsl:otherwise></xsl:choose>

• xsl:choose is often used within an xsl:for-each loop

xsl:sort

• You can place an xsl:sort inside an xsl:for-each

• The attribute of the sort tells what field to sort on

• Example: <ul> <xsl:for-each select="//book"> <xsl:sort select="author"/> <li> <xsl:value-of select="title"/> by <xsl:value-of select="author"> </li> </xsl:for-each> </ul>

• This example creates a list of titles and authors, sorted by author

Reference

• For detail XSLT element visit; http://www.w3schools.com/xsl/xsl_w3celementref.asp

Chapter Overview

• Basic of XSL Transformation (XSLT)• Using XSLT Template and match • Using apply-templates• Using Select• Using Multiple Templates• Using Looping Statement• Filtering and Sorting XML Data• Using output method• XSLT Issues

Using output method

• Tells XSL processor how to format output• • The default for the method attribute is chosen as follows. If any

of the following conditions are true, the default output method is "html":

– The root node of the result tree has an element child.– the result tree has local part "html" (in any combination of uppercase

and lowercase) and a null namespace URI.– Any text nodes preceding the first element child of the root node of

the result tree contain only white space characters.

• Otherwise, the default output method is "xml". The default output method should be used if there are no <xsl:output> elements or if none of the <xsl:output> elements specifies a value for the method attribute.

Using output method

• <xsl:output method=“xml or html or text”version=“version”encoding=“encoding”omit-xml-declaration=“yes or no”standalone=“yes or no”cdata-section-elements=“CDATA

sections”indent=“yes or no”/>

• Must be top-level element below <xsl:stylesheet…>

Using output method

• method– Specifies whether XML, HTML, or Text is produced– XSL processor can support other types of output if desired

• If <xml:output …> not specified, method is:– HTML if the root element of the result tree is <HTML>– XML otherwise

• XSL processor can do special things based on the method– Change <BR/> for XML-compliant documents to <BR> for

HTML documents

Using output method

• version is the XML version– Specifies version 1.0 in relation to the "xml" output method

• encoding is the suggested encoding value– XSL processor may use it– Specifies the preferred character encoding that the parser

should use

• standalone=“yes” indicates that there are no external references– Specifies whether the XSLT processor should output a

standalone document declaration

Using output method

• omit-xml-declaration– As it states: omits XML declaration statement from

the output– Might be for a number of reasons– Output document could be:

• Included in another document• An XML-stylesheet

– Default is “no” if the method is XML

• indent– “no” specifies that no indenting will occur - tags will

be run on in some XSL processors– “yes” specifies that pretty printing will occur

output method=“xml”

output method=“html”

output method=“text”

Chapter Overview

Basic of XSL Transformation (XSLT) Using XSLT Template and match Using apply-templates Using Select Using Multiple Templates Using Looping Statement Filtering and Sorting XML Data Using output method XSLT Issues

XSLT Isssues

• Confusing syntax and semantics– Like Prolog+C+XML – It's really a programming language, but using markup

language syntax

• Hard to debug– XSL Trace helps a little

• Don't have full power of, say, Java inside templates– No database access, hashtables, methods, objects, etc.

• Still need separate .xsl file for each client device.