XSLT.ppt

KGSCSEPSGCT 280 views 43 slides Apr 22, 2022
Slide 1
Slide 1 of 43
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43

About This Presentation

XML Style Sheet Language Transformation


Slide Content

XSL

•XSLstands for EXtensible Stylesheet
Language, and is a style sheet language
for XML documents.
•XSLTstands for XSL Transformations.

What is XSL?
•XSL is the style sheet languagefor XML
•Style sheets are typically used to specify
how information should be displayed.
•They lead to separation of document's
content from presentational information.

XSL Languages
XSL consists of three parts:
•XSLT is a language for transforming
XML documents
•XPath is a language for defining parts of
an XML document
•XSL-FO is a language for formatting XML
documents

What is XSLT?
•XSLT is the most important part of XSL
•XSLT transforms an XML document
into another XML document
•XSLT uses XPathto navigate in XML
documents
•XSLT is a W3C Recommendation

History
•XSL is derived from DSSSL
–Document Style and Semantics Specification
Language
•The style sheet language for SGML
•Based on the Scheme (Lisp) programming language
–XSL uses XML syntax instead of Scheme

XSL Architecture
Source
XML doc
XSL
stylesheet
XSL
processor
Target
Document

How does XSL work?
•The XSL processor parses the source
XML document
•Templates in the stylesheetare matched
against patterns in the source document
•The templates are expanded, producing
fragments of the target document
•This process is (typically) applied
recursively until all pattern-matcheshave
been exhausted
•The complete target documentis
produced as output

XSLT -Transformation
•Correct Style Sheet Declaration
•The root element that declares the
document to be an XSL style sheet is
<xsl:stylesheet> or <xsl:transform>.
<xsl:stylesheetversion="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Or
<xsl:transformversion="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

cdcatalog.xmlcdcatalog.xsl

Create an XSL Style Sheet
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheetversion="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:templatematch="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<trbgcolor="#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>
"cdcatalog.xsl"

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="ISO-8859-1"?>
<?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> View the result

1. XSLT <xsl:template> Element
•An XSL style sheet consists of one or
more set of rulesthat are called
templates.
•A template contains rules to apply when a
specified node is matched.

The <xsl:template> Element
•The <xsl:template> element is used to
build templates.
•The matchattribute is used to associate a
template with an XML element.
•The match attribute can also be used 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).

<?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>
<tr>
<td>.</td>
<td>.</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
1cdcatalog.xsl

1cdcatalog.xml
1cdcatalog.xsl
View the result

2. XSLT <xsl:value-of> Element
•The <xsl:value-of> element is used to
extract the value of a selected node.

<?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>
<tr>
<td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
xsl value-of.xsl

1cdcatalog.xml xsl value-of.xsl
View the Result

3. XSLT <xsl:for-each> Element
•The <xsl:for-each> element allows you to
do looping in XSLT.
•The XSL <xsl:for-each> element can be
used to select every XML element of a
specified node-set:

<?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>
XSL for-each.xsl

1cdcatalog.xml XSL for-each.xsl
View the result

Filtering the Output
<xsl:for-eachselect="catalog/cd[artist='Bob Dylan']">
Legal filter operators are:
=(equal)
!= (not equal)
&lt; less than
&gt; greater than

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheetversion="1.0“
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:templatematch="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<trbgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-eachselect="catalog/cd[artist='Bob Dylan']">
<tr>
<td> <xsl:value-ofselect="title"/> </td>
<td> <xsl:value-ofselect="artist"/> </td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

4. XSLT <xsl:sort> Element
•The <xsl:sort> element is used to sort the
output.

<?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:sort select="artist"/>
<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>
XSL sort.xsl

1cdcatalog.xml XSL sort.xsl
View the result

5. XSLT <xsl:if> Element
•The <xsl:if> element is used to put a conditional
test against the content of the XML file.
•To put a conditional if test against the content of
the XML file, add an <xsl:if> element to the XSL
document.
•Syntax
<xsl:iftest="expression">
...some output if the expression is true...
</xsl:if>

Where to Put the <xsl:if> Element
<?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>

1cdcatalog.xml XSL:if.xsl
View the result

6. XSLT <xsl:choose> Element
•The <xsl:choose> element is used in
conjunction with <xsl:when> and
<xsl:otherwise> to express multiple
conditional tests.

The <xsl:choose> Element
Syntax
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>

Where to put the Choose Condition
•To insert a multiple conditional test against
the XML file, add the <xsl:choose>,
<xsl:when>, and <xsl:otherwise> elements
to the XSL file:

<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 &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>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
XSL choose.xsl

•The code above will add a pink background-
color to the "Artist" column WHEN the price
of the CD is higher than 10.
1cdcatalog.xml XSL:choose.xsl
View the result

XSLT <xsl:apply-templates>
Element
•The <xsl:apply-templates> element
applies a template to the current element
or to the current element's child nodes.

<?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:#ff0000">
<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>

1cdcatalog.xml XSL:ApplyTemplates.xsl
View the result
Tags