Upload
surinder23
View
232
Download
1
Embed Size (px)
DESCRIPTION
XML STYLESHEET LANGUAGE TRANSFORMATIONS
Citation preview
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.
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>
<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>
Effect of Applying Stylesheet
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"?>
ex_css.css
title{font-size:10;color:red;font-family:tahoma}
artist{font-size:24;color:blue}
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
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">
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>
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
Sample Template
<xsl:template match="title">
Title: <span style="color:blue">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
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.
for-each Element
Loop construct in XSLT
Eg:<xsl:for-each select="catalog/cd">
…..
</xsl:for-each>
<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.
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>
Effect of Applying Stylesheet
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.
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>
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.
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.
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 > 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>
Multiple Conditional Tests
Apply “when”, “otherwise” and “choose” element
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>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price > 10">
<td bgcolor="yellow">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:when test="price > 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>
Creating Element/Attribute
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.
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>
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>
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>
Copying
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!
Intro.xml<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>
<myMessage>
<message>Welcome to XSLT!</message>
</myMessage>
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 'Hi World' for a change!
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
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>
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.
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>
result.xml<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>
<myMessage>
<message>Welcome to XSLT!</message>
</myMessage>
Combining Stylesheets
import Element
local template has higher precedence than the imported template
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>
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>
Transformed xml<html>
<body>
<h2>Deitel's XML Primer</h2>
<br>
<p>
Author: Deitel, Paul
</p>
</body>
</html>
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.
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>
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>
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>
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>
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