54
XSLT

XSLT

Embed Size (px)

DESCRIPTION

XML STYLESHEET LANGUAGE TRANSFORMATIONS

Citation preview

Page 1: XSLT

XSLT

Page 2: XSLT

Introduction

Extensible Stylesheet Language Transformations.

XSLT transforms an XML document into another XML document(generally more expressive)

XSLT uses XPath to navigate in XML documents

Itself is an XML document with a root element stylesheet.

Page 3: XSLT

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

<!-- Edited by XMLSpy® -->

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

<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>

<title>Hide your heart</title>

<artist>Bonnie Tyler</artist>

<country>UK</country>

<company>CBS Records</company>

<price>9.90</price>

<year>1988</year>

</cd>

Page 4: XSLT

<cd><title>Greatest Hits</title>

<artist>Dolly Parton</artist>

<country>USA</country>

<company>RCA</company>

<price>9.90</price>

<year>1982</year>

</cd>

<cd><title>Still got the blues</title>

<artist>Gary Moore</artist>

<country>UK</country>

<company>Virgin records</company>

<price>10.20</price>

<year>1990</year>

</cd>

<cd><title>Eros</title>

<artist>Eros Ramazzotti</artist>

<country>EU</country>

<company>BMG</company>

<price>9.90</price>

<year>1997</year>

</cd>

<cd><title>Maggie May</title>

<artist>Rod Stewart</artist>

<country>UK</country>

<company>Pickwick</company>

<price>8.50</price>

<year>1990</year></cd>

<cd><title>Romanza</title><artist>Andrea Bocelli</artist><country>EU</country><company>Polydor</company><price>10.80</price><year>1996</year></cd><cd><title>Black angel</title><artist>Savage Rose</artist><country>EU</country><company>Mega</company><price>10.90</price><year>1995</year></cd><cd><title>1999 Grammy Nominees</title><artist>Many</artist><country>USA</country><company>Grammy</company><price>10.20</price><year>1999</year></cd><cd><title>For the good times</title><artist>Kenny Rogers</artist><country>UK</country><company>Mucik Master</company><price>8.70</price><year>1995</year></cd>

Page 5: XSLT

Effect of Applying Stylesheet

Page 6: XSLT

Using CSS

The information can not be rearranged(sorting etc.)

Information encoded in attributes can not be exploited.

Additional structures can not be introduced.

<?xml-stylesheet type="text/css" href="ex_css.css"?>

Page 7: XSLT

ex_css.css

title{font-size:10;color:red;font-family:tahoma}

artist{font-size:24;color:blue}

Page 8: XSLT
Page 9: XSLT

XSLT

To transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML

Overcomes the limitations of the CSS can add(remove) elements and attributes to

( from) the output file. can also rearrange elements can perform tests and make decisions about

which elements to hide and display

Page 10: XSLT

Declaration

Root element is “stylesheet”

Namespace: http://www.w3.org/1999/XSL/Transform

To use this namespace, it is required to include the attribute version="1.0".

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

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

Page 11: XSLT

Linking XSLT to XML Doc.

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

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

<root_element>….

….

….

</root_element>

Page 12: XSLT

Template ElementTemplate is set of rules

The rules defined by template are applied when the specified node is matched

Syntax: <xsl:template>

Attribute: The match attribute is used to associate a template

with an XML element. Value of match is an XPath expression. Eg: <xsl:template match="/">

For root element

<xsl:template match=" catalog/cd /title">To select title element

Page 13: XSLT

Sample Template

<xsl:template match="title">

Title: <span style="color:blue">

<xsl:value-of select="."/></span>

<br />

</xsl:template>

Page 14: XSLT

value-of Elementto extract the value of a selected node and add it to the output stream of the transformation

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

select attribute: Value is “XPath Expression” text contents of the node-set returned by the

XPath expression are placed in the result tree.

Page 15: XSLT

for-each Element

Loop construct in XSLT

Eg:<xsl:for-each select="catalog/cd">

…..

</xsl:for-each>

Page 16: XSLT

<xsl:for-each select="catalog/cd">    <xsl:value-of select="title"/> <xsl:value-of select="artist"/>

</xsl:for-each>

The above piece of code will loop over all the “cd” elements of “catalog” element.

For each of the cd elements the value of element “title” and element “artist” will be placed in the result tree.

Page 17: XSLT

ex1.xsl<?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> <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="title"/></td>

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

</tr>

</xsl:for-each>

</table>

</body> </html>

</xsl:template>

</xsl:stylesheet>

Page 18: XSLT

Effect of Applying Stylesheet

Page 19: XSLT

apply-template Element

applies a template to the current element or to the current element's child nodes.

select attribute:  it will process only the child element that

matches the value of the attribute. It can be used to specify the order in which

child nodes are processed.

Page 20: XSLT

ex_apply.xsl<?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> <body>

<h2>My CD Collection</h2>

<xsl:apply-templates/>

</body> </html>

</xsl:template>

<xsl:template match="cd">

<p>

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

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

</p>

</xsl:template>

<xsl:template match="title">

Title: <span style="color:blue">

<xsl:value-of select="."/></span> <br />

</xsl:template>

<xsl:template match="artist">

Artist: <span style="color:#00ff00">

<xsl:value-of select="."/></span> <br />

</xsl:template> </xsl:stylesheet>

Page 21: XSLT
Page 22: XSLT

sort ElementSorts the output

Eg:<xsl:for-each select="catalog/cd">

      <xsl:sort select="artist“ order=“ascending”/>      <tr>        <td><xsl:value-of select="title"/></td>        <td><xsl:value-of select="artist"/></td>      </tr>

</xsl:for-each>

The result tree will have cd elements in the ascending order of element artist.

Page 23: XSLT
Page 24: XSLT

if Element

put a conditional test against the content of the XML file

Syntax:<xsl:if test=“test_expression">

  ...some output if the expression is true...

</xsl:if>

test attribute: Specifies the expression to be evaluated.

Page 25: XSLT

ex_if.xsl<?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> <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="title"/></td>

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

</tr>

</xsl:if>

</xsl:for-each>

</table>

</body> </html>

</xsl:template>

</xsl:stylesheet>

Page 26: XSLT
Page 27: XSLT

Multiple Conditional Tests

Apply “when”, “otherwise” and “choose” element

Page 28: XSLT

ex_2.xsl

<?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>

<body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Artist</th>

</tr>

Page 29: XSLT

<xsl:for-each select="catalog/cd">

<tr>

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

<xsl:choose>

<xsl:when test="price &gt; 10">

<td bgcolor="yellow">

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

</xsl:when>

<xsl:when test="price &gt; 9">

<td bgcolor="green">

<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:stylesheet>

Page 30: XSLT
Page 31: XSLT

Creating Element/Attribute

Page 32: XSLT

Element “element” is used to create an element.

Attribute “name” is used to specify the name of the newly created element.

Value of attribute name is an XPath expression.

Page 33: XSLT

Example

<?xml version = "1.0"?>

<sports>

<game title = "cricket">

<id>243</id>

<para> More popular among commonwealth nations. </para>

</game>

<game title = "baseball">

<id>431</id>

<para> More popular in America. </para>

</game>

<game title = "soccer">

<id>123</id>

<para>28 Most popular sport in the world. </para>

</game>

</sports>

Page 34: XSLT

stylesheet<?xml version = "1.0"?>

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

<xsl:template match = "/">

<xsl:apply-templates/>

</xsl:template>

<xsl:template match = "sports">

<sports>

<xsl:apply-templates/>

</sports>

</xsl:template>

<xsl:template match = "game">

<xsl:element name = "{@title}">

<xsl:attribute name = "id">

<xsl:value-of select = "id"/>

</xsl:attribute>

<comment>

<xsl:value-of select = "para"/>

</comment>

</xsl:element>

</xsl:template>

</xsl:stylesheet>

Page 35: XSLT

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

<sports>

<cricket id = "243">

<comment> More popular among commonwealth nations. </comment>

</cricket>

<baseball id = "431">

<comment> More popular in America. </comment>

</baseball>

<soccer id = "123">

<comment> Most popular sport in the world. </comment>

</soccer>

</sports>

Page 36: XSLT

Copying

Page 37: XSLT

To duplicate nodes from the source tree into the result tree

Element “copy” is used to produce a copy of the context node and place it in the result tree. Syntax: <xsl:copy>  child nodes and attributes of the current node

are not automatically copied!

Page 38: XSLT

Intro.xml<?xml version = "1.0"?>

<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>

<myMessage>

<message>Welcome to XSLT!</message>

</myMessage>

Page 39: XSLT

Intro.xsl<?xml version = "1.0"?>

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

<xsl:template match = "myMessage">

<xsl:copy>

<xsl:apply-templates/>

</xsl:copy>

</xsl:template>

<xsl:template match = "message">

<xsl:copy>

How about &apos;Hi World&apos; for a change!

</xsl:copy>

</xsl:template>

</xsl:stylesheet>

Page 40: XSLT

Result.xml

<?xml version = "1.0"?>

<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>

<myMessage>

<message> How about ‘Hi World’ for a change! </message>

</myMessage>

Page 41: XSLT

copy-of Element

 creates a copy of the current node.

Namespace nodes, child nodes, and attributes of the current node are automatically copied as well

This element can be used to insert multiple copies of the same node into different places in the output.

Page 42: XSLT

stylesheet

<?xml version = "1.0"?>

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

<xsl:template match = "myMessage">

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

</xsl:template>

</xsl:stylesheet>

Page 43: XSLT

result.xml<?xml version = "1.0"?>

<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>

<myMessage>

<message>Welcome to XSLT!</message>

</myMessage>

Page 44: XSLT

Combining Stylesheets

Page 45: XSLT

import Element

local template has higher precedence than the imported template

Page 46: XSLT

usage2.xsl<?xml version = "1.0"?>

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

<xsl:template match = "book">

<html>

<body>

<xsl:apply-templates/>

</body>

</html>

</xsl:template>

<xsl:template match = "title">

<xsl:value-of select = "."/>

</xsl:template>

<xsl:template match = "author"><br/>

<p>Author: <xsl:value-of select = "lastName"/>,

<xsl:value-of select = "firstName"/> </p>

</xsl:template>

<xsl:template match = "*|text()"/>

</xsl:stylesheet>

Page 47: XSLT

usage1.xsl<?xml version = "1.0"?>

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

<xsl:import href = "usage2.xsl"/>

<xsl:template match = "title">

<h2>

<xsl:value-of select = "."/>

</h2>

</xsl:template>

</xsl:stylesheet>

Page 48: XSLT

Transformed xml<html>

<body>

<h2>Deitel's XML Primer</h2>

<br>

<p>

Author: Deitel, Paul

</p>

</body>

</html>

Page 49: XSLT

include Element

Include element can be used to refer to another xslt doc.

The attribute “href” specifies the path of the xsl doc to be included.

The local and the included both the templates have the same precedence.

If any conflict arises, the template that last occurs is used.

Page 50: XSLT

usage.xsl<?xml version = "1.0"?>

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

<xsl:template match = "/">

<html>

<body>

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

</body>

</html>

</xsl:template>

<xsl:template match = "book">

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

<xsl:apply-templates/>

</xsl:template>

<xsl:include href = "author.xsl"/>

<xsl:include href = "chapters.xsl"/>

<xsl:template match = "*|text()"/>

</xsl:stylesheet>

Page 51: XSLT

author.xsl<?xml version = "1.0"?>

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

<xsl:template match = "author">

<p>Author:

<xsl:value-of select = "lastName"/>,

<xsl:value-of select = "firstName"/>

</p>

</xsl:template>

</xsl:stylesheet>

Page 52: XSLT

chapters.xsl<?xml version = "1.0"?>

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

<xsl:template match = "chapters">

Chapters:

<ul>

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

</ul>

</xsl:template>

<xsl:template match = "chapter">

<li>

<xsl:value-of select = "."/>

</li>

</xsl:template>

</xsl:stylesheet>

Page 53: XSLT

result.xml<html>

<body>

<h2>Deitel’s XML Primer</h2>

<p>Author:

Deitel, Paul</p>

Chapters:

<ul>

<li>Easy XML</li>

<li>XML Elements?</li>

</ul>

</body>

</html>

Page 54: XSLT

Variables

variable element is used to declare a local or global variable. global variable: declared as a top-level element local variable: declared within a template

Once variable's value is set,it cannot be changed or modifued

set value to a variable by the content of the <xsl:variable> element or by the select attribute