XSLT
Introduction
Extensible Stylesheet Language
Transformations.
XSLT transforms an XML document into
another XML document(gen...
Example-01
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<?xml-stylesheet type="text/xsl" href="e...
<cd><title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</...
Effect of Applying Stylesheet
Using CSS
The information can not be
rearranged(sorting etc.)
Information encoded in attributes can not
be exploited.
Addi...
ex_css.css
title{font-size:10;color:red;fontfamily: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,
...
Declaration
Root element is “stylesheet”
Namespace:


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

To use this namespace, it is ...
Linking XSLT to XML Doc.
<?xml version="1.0" encoding="ISO-8859-1"?>

<?xml-stylesheet type="text/xsl" href=“abc.xsl"?>
<r...
Template Element
Template is set of rules
The rules defined by template are applied when the
specified node is matched
Syn...
Sample Template
<xsl:template match="title">
Title: <span style="color:blue">
<xsl:value-of select="."/></span>
<br />
</x...
value-of Element
to extract the value of a selected node and
add it to the output stream of the
transformation
Eg:


<xsl...
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 abov...
ex1.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tr...
Effect of Applying Stylesheet
apply-template Element
applies a template to the current element
or to the current element's child nodes.
select attribute...
ex_apply.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/X...
sort Element
Sorts the output
Eg:
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist“
order=“ascending”/>
<tr>
<t...
if Element
put a conditional test against the content
of the XML file
Syntax:
<xsl:if test=“test_expression">
...some outp...
ex_if.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/...
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/T...
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price &gt; 10...
Creating
Element/Attribute
Element “element” is used to create an
element.
Attribute “name” is used to specify the
name of the newly created element....
Example
<?xml version = "1.0"?>
<sports>
<game title = "cricket">
<id>243</id>
<para> More popular among commonwealth nati...
stylesheet
<?xml version = "1.0"?>
<xsl:stylesheet version = "1.0“ xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xs...
result.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<sports>
<cricket id = "243">
<comment> More popular among commonwea...
Copying
To duplicate nodes from the source tree
into the result tree
Element “copy” is used to produce a
copy of the context node ...
Intro.xml
<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>
<myMessage>
<message>Welcome to ...
Intro.xsl
<?xml version = "1.0"?>
<xsl:stylesheet version = "1.0“ xmlns:xsl = “http://www.w3.org/1999/XSL/Transform">
<xsl...
Result.xml
<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>
<myMessage>
<message> How about...
copy-of Element
creates a copy of the current node.
Namespace nodes, child nodes, and
attributes of the current node are
a...
stylesheet
<?xml version = "1.0"?>
<xsl:stylesheet version = "1.0”
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:...
result.xml
<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>
<myMessage>
<message>Welcome to...
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:...
usage1.xsl
<?xml version = "1.0"?>
<xsl:stylesheet version = "1.0”
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:...
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 x...
usage.xsl
<?xml version = "1.0"?>
<xsl:stylesheet version = "1.0“ xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl...
author.xsl
<?xml version = "1.0"?>
<xsl:stylesheet version = "1.0“ xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xs...
chapters.xsl
<?xml version = "1.0"?>
<xsl:stylesheet version = "1.0“ xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<...
result.xml
<html>
<body>
<h2>Deitel‟s XML Primer</h2>
<p>Author:
Deitel, Paul</p>
Chapters:
<ul>
<li>Easy XML</li>
<li>XML...
Variables
variable element is used to declare a local
or global variable.



global variable: declared as a top-level el...
XSLT
XSLT
XSLT
XSLT
XSLT
Upcoming SlideShare
Loading in …5
×

XSLT

398 views
310 views

Published on

XML STYLESHEET LANGUAGE TRANSFORMATIONS

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
398
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
25
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

XSLT

  1. 1. XSLT
  2. 2. 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.
  3. 3. 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>
  4. 4. <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> <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>
  5. 5. Effect of Applying Stylesheet
  6. 6. 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"?>
  7. 7. ex_css.css title{font-size:10;color:red;fontfamily:tahoma} artist{font-size:24;color:blue}
  8. 8. 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
  9. 9. 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">
  10. 10. 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>
  11. 11. Template Element Template 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
  12. 12. Sample Template <xsl:template match="title"> Title: <span style="color:blue"> <xsl:value-of select="."/></span> <br /> </xsl:template>
  13. 13. value-of Element to 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.
  14. 14. for-each Element Loop construct in XSLT Eg: <xsl:for-each select="catalog/cd"> ….. </xsl:for-each>
  15. 15. <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.
  16. 16. 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>
  17. 17. Effect of Applying Stylesheet
  18. 18. 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.
  19. 19. 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>
  20. 20. sort Element Sorts 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.
  21. 21. 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.
  22. 22. 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>
  23. 23. Multiple Conditional Tests Apply “when”, “otherwise” and “choose” element
  24. 24. 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>
  25. 25. <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>
  26. 26. Creating Element/Attribute
  27. 27. 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.
  28. 28. 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>
  29. 29. 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>
  30. 30. 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>
  31. 31. Copying
  32. 32. 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!
  33. 33. Intro.xml <?xml version = "1.0"?> <?xml:stylesheet type = "text/xsl" href = "intro.xsl"?> <myMessage> <message>Welcome to XSLT!</message> </myMessage>
  34. 34. 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>
  35. 35. 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>
  36. 36. 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.
  37. 37. 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>
  38. 38. result.xml <?xml version = "1.0"?> <?xml:stylesheet type = "text/xsl" href = "intro.xsl"?> <myMessage> <message>Welcome to XSLT!</message> </myMessage>
  39. 39. Combining Stylesheets
  40. 40. import Element local template has higher precedence than the imported template
  41. 41. 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>
  42. 42. 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>
  43. 43. Transformed xml <html> <body> <h2>Deitel's XML Primer</h2> <br> <p> Author: Deitel, Paul </p> </body> </html>
  44. 44. 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.
  45. 45. 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>
  46. 46. 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>
  47. 47. 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>
  48. 48. 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>
  49. 49. 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

×