0% found this document useful (0 votes)
22 views20 pages

XSL & XSLT

Uploaded by

Dewang Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views20 pages

XSL & XSLT

Uploaded by

Dewang Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

XSL

XSL
• XSL - Extensible stylesheet language
• style sheets for XML documents
• XSL describes how the XML document should be displayed
• XSL consists of four parts:
– XSLT - a language for transforming XML documents
– XPath - a language for navigating in XML documents
– XSL- FO - a language for formatting XML documents
– XQuery - a language for querying XML documents
XSLT
XSLT
XSLT
• With XSLT we can,
– add/remove elements and attributes to or
from the output file

– rearrange and sort elements

– perform tests and make decisions about


which elements to hide and display
XSLT Declaration
• The root element that declares the
document to be an XSL style sheet is:
<xsl:stylesheet> or <xsl:transform>

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

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

– Create XML file


(filename.xml)

– Create XSL file


(filename.xsl)

– Link XSL file to XML


document
cdcatalog.xml
We want to transform the following XML document ("cdcatalog.xml") into XHTML

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


<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>
cdcatalog.xsl
<?xml version="1.0" encoding="UTF-8"?>
<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>
Link the XSL Style Sheet to the XML Document
• Add the XSL style sheet reference to your XML document ("cdcatalog.xml"):

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


<?xml-stylesheet type="text/xsl" href="cdcatalog.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>
.
.
</catalog>
XSLT
• <xsl:template>
– An XSL style sheet consists of one or more set of rules that
are called templates
– A template contains rules to apply when a specified
node is matched
– <xsl:template> element is used to build templates
– The match attribute is used to associate a template with an
XML element and to define a template for the entire XML
document
– The value of the match attribute is an XPath expression
(i.e. match="/" defines the whole document).
XSLT
• Example:- cdcatalog.xsl

• XSL style sheet is an XML document, it always begins with the XML
declaration: <?xml version="1.0" encoding="UTF-8"?>.
• The next element, <xsl:stylesheet>, defines that this document is an
XSLT style sheet document (along with the version number and XSLT
namespace attributes).
• The <xsl:template> element defines a template. The match="/"
attribute associates the template with the root of the XML source
document.
• The content inside the <xsl:template> element defines some HTML to
write to the output.
• The last two lines define the end of the template and the end of the
style sheet
XSLT
• <xsl:value-of>

– The <xsl:value-of> element is used to


extract the value of a selected node.

– Example
• <xsl:value-of select="catalog/cd/title"/>
• <xsl:value-of select="catalog/cd/artist"/>
XSLT
• <xsl:for-each> Element
- <xsl:for-each> element can be used to select every XML element of a
specified node-set

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

Filtering the Output


• We can also filter the output from the XML file by adding a criterion to the select
attribute in the <xsl:for-each> element.
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
• Legal filter operators are:
= (equal)
!= (not equal)
&lt; less than
&gt; greater than
XSLT
• <xsl:sort>
– <xsl:sort> element is used to sort the output
– To sort the output, simply add an <xsl:sort>
element inside the <xsl:for-each> element in
the XSL file:
– Example
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<xsl:value-of select="title"/>
</xsl:for-each>
• <xsl:if>
XSLT
– <xsl:if> element is used to put a conditional test
– <xsl:if test="expression">
……..
……..
</xsl:if>
– Example
<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>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:if>
</xsl:for-each>
XSLT
• <xsl:choose>
– <xsl:choose> element is used to express
multiple conditional tests <xsl:when> and
<xsl:otherwise>
– <xsl:choose>
<xsl:when test="expression">
.....
</xsl:when>
<xsl:otherwise>
.....
</xsl:otherwise>
</xsl:choose>
XSLT
• <xsl:choose>
• Example
<xsl:choose>
<xsl:when test="price &gt; 10">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
XSLT
• <xsl:apply-templates>

– <xsl:apply-templates> element applies a


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

– Adding a select attribute to the <xsl:apply-


templates> element will process only the
child element that matches the value of the
attribute.
XSLT
• <xsl:copy> and <xsl:copy-of>
<xsl:template match=“OL”>
<xsl:copy-of select=“.” />
<!-- this will copy the OL tag and all of its
subelements -->
</xsl:template>

<xsl:template match=“OL”>
<xsl:copy />
<!-- this will copy the OL tag but leave all of its
subelements out -->
</xsl:template>

<xsl:copy-of …> expects a select=“expression”


argument, and copies the result of expression.

You might also like