Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

eXtensible Stylesheet Language

Transformations
1
XSL
2

 XSL stand for eXtensible Stylesheet Language

 It is a style sheet application created specifically for XML.

 Features of XSL include:

– It provides a transformation language (XSLT).

– XSL can be used as a formatting language.

– XSL can be used to sort and filter.

– XSL can be used for pattern matching to find records.


XSL
3

 XSL consist of three languages:

– XSL Transformations (XSLT)

– XML Path Language (XPATH) a language for navigating


the XML document.

– XSL Formatting Objects (XSL-FO) – an XML Language


for formatting XML documents.
XSL
4

XML

HTML

XSL

XSL
CSS
HTML Document
displayed in the web
XSL - Style Sheet Declaration
5

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

OR
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
XSL - Style Sheet Declaration
6

 The <xsl:transform> element holds the same syntactic

value as <xsl:stylesheet> . However, <xsl:stylesheet>


element is used more commonly than <xsl:transform> element.

 To get access to the XSLT elements, attributes and features we must

declare the XSLT namespace at the top of the document.

 The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to

the official W3C XSLT namespace. If you use this namespace, you
must also include the attribute version="1.0".
Link the XSL Style Sheet to the XML
7

 To link the XSL Style Sheet with the XML, an

processing instruction is used

<?xml-stylesheet type="text/xsl" href= "Stylesheet.xsl"?>
Example for XSL
8
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="breakfast_menu">
<html>
<title></title>
<body>
<table border="1">
<thead style="text-align:left" width="50%">
<td>Name</td>
<td>Price</td>
</thead>
<xsl:for-each select="food">
<xsl:sort select="price" order="ascending"/>
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Difference Between XSL and CSS
9

CSS work by assigning a XSL provides means of


set of display properties transforming of XML
to an HTML element documents
XSLT lets us to map a
CSS determines the visual
certain pattern in the
appearance of a page
source document
It transforms XML into
It does not change the
structures such as lists or
structure of the document
tables
Patterns
10

 Pattern matching is the process of using patterns to identify

nodes in the tree that are to be processed according to XSL


styles

Pattern Pattern

Source document Result tree


Patterns
11

 The patterns supported in XSL are:

– Sorting
– Operators
– Filtering
Selecting Nodes
12

Expression Description
nodename Selects all nodes with the name "nodename"
/ Selects from the root node
// Selects nodes in the document from the current node that
match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes
Filtering and Logical Operators
13

 Filter operations can contain expression such as Boolean


expression, AND, OR, and NOT expressions
Operator Description
AND Logical and
OR Logical or
NOT Negation
= Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal
Templates
14

 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 match="/">
<!– XSL content go here -->
</xsl:template>

 The template has two parts:

– The matching part


– The processing part
Types of Matching
15
xsl:import and xsl:include
16

 Style sheets created by other developers can be imported using the

xsl:import

 The syntax for importing style sheets is:

<xsl:import href= “another stylesheet.xsl”/>


<xsl:import href= “another stylesheet1.xsl”/>

 All the different style sheets imported are arranged in an import tree.

 A new node is created in the import tree when a style

sheet is imported
xsl:value-of
17

 • The <xsl:value-of> element is used to extract the

value of a selected node.


• The <xsl:value-of> element can only have one
node assigned to it

<xsl:value-of select="name"/>
xsl:for-each
18

 The XSL <xsl:for-each> element can be used to

select every XML element of a specified node-set.

<xsl:for-each select="food">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
xsl:text
19

 The <xsl:text> element is used to write literal text

to the output.

<xsl:text>This is literal text</xsl:text>


xsl:number
20

 The <xsl:number> element is used to determine

the integer position of the current node in the


source. It is also used to format a number.

<xsl:number count="expression" value="expression" format="formatstring”/>


xsl:if
21

 To add a conditional test, add the <xsl:if> element

inside the <xsl:for-each> element in the XSL file

<xsl:if test="price &gt; 8">


<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
</xsl:if>
xsl:choose
22

 The <xsl:choose> element is used in conjunction with <xsl:when> and

<xsl:otherwise> to express multiple conditional tests

<xsl:choose>
<xsl:when test="price &gt; 8">
<td style="color:red"><xsl:value-of select="name"/></td>
<td style="color:red"><xsl:value-of select="price"/></td>

</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
</xsl:otherwise>
</xsl:choose>
xsl:sort
23

 The <xsl:sort> element is used to sort the output.

<xsl:sort select="price" order="ascending"/>


Handling Expressions
24

 XSL supports five types of Expressions. These are:

– Node Sets
– Booleans
– Strings
– Numbers
Node Sets
25

 Node set functions take a node set argument. They

return a node set, or information about a particular


node within a node set.

 Node set functions are: name(), local-name(),

namespace-uri(), root().
Node Sets
26

Name Description
fn:name() Returns the name of the current node or the
fn:name(nodeset) first node in the specified node set
fn:local-name() Returns the name of the current node or the
fn:local- first node in the specified node set – without
name(nodeset) the namespace prefix
fn:namespace-uri()
fn:namespace- Returns the namespace URI of the current
uri(nodeset) node or the first node in the specified node set

fn:root() Returns the root of the tree to which the


fn:root(node) current node or the specified belongs. This will
usually be
Booleans
27

Name Description
fn:boolean(arg) Returns a boolean value for a number, string, or node-
set
The argument is first reduced to a Boolean value by
applying the boolean() function. Returns true if the
fn:not(arg) boolean value is false, and false if the boolean value is
true
Example: not(true())
Result: false
Returns the boolean value true
fn:true() Example: true()
Result: true
Returns the boolean value false
fn:false() Example: false()
Result: false
Strings
28

 String functions are used to evaluate, format, and

manipulate string arguments, or to convert an object


to a string.

 The different string functions are: string(),

compare(), concat(), string-length() and substring().


Strings
29

Name Description
Returns the string value of the argument. The
argument could be a number, boolean, or
fn:string(arg) node-set
Example: string(314)
Result: "314"
Returns -1 if comp1 is less than comp2, 0 if
comp1 is equal to comp2, or 1 if comp1 is
fn:compare(comp1,comp2) greater than comp2 according to the rules of
the collation that is used)
Example: compare('ghi', 'ghi')
Result: 0
fn:concat(string,string, Returns the concatenation of the strings
...) Example: concat('XPath ','is ','FUN!')
Result: 'XPath is FUN!'
Strings
30

Name Description
Returns the substring from the start position to
fn:substring(string,start the specified length. Index of the first character
,len) is 1. If length is omitted it returns the substring
fn:substring(string,start from the start position to the end
) Example: substring('Beatles',1,4)
Result: 'Beat’
Returns the length of the specified string. If
fn:string-length(string) there is no string argument it returns the length
fn:string-length() of the string value of the current node
Example: string-length('Beatles')
Result: 7
Converts string1 by replacing the characters in
fn:translate(string1,stri string2 with the characters in string3
ng2,string3) Example: translate('12:30','0123','abcd')
Result: 'bc:da'
Numbers
31

 Number functions return strings or numbers and can

be used with comparison operators in filter patterns.

 The different number functions are: number(),

ceilling(), floor(), and round().


Numbers
32

Name Description
Returns the numeric value of the argument. The argument could be a
fn:number(arg) boolean, string, or node-set
Example: number('100')
Result: 100
Returns the absolute value of the argument
fn:abs(num) Example: abs(-3.14)
Result: 3.14
Returns the smallest integer that is greater than the number argument
fn:ceiling(num) Example: ceiling(3.14)
Result: 4
Returns the largest integer that is not greater than the number argument
fn:floor(num) Example: floor(3.14)
Result: 3
Rounds the number argument to the nearest integer
fn:round(num) Example: round(3.14)
Result: 3
Q&A
33

You might also like