Sie sind auf Seite 1von 32

WTA Topic 4 :

Extensible Stylesheet
Language (XSL)
1

Topics:


XSL







Learn how XSL can transform XML documents into other


formats such as HTML.
XSL stylesheet consists of a set of rules called
templates.
Use XSLT to output an XML source document into an
XML result document.
Use XPATH expressions in matching patterns to locate
parts of an XML document.
Sorting and filtering xml data (using XSLT).
Creating Conditional Statements.

Introduction







XSL stands for EXtensible Stylesheet Language,


and is a style sheet language for XML documents.
XSLT stands for XSL Transformations.
How to use XSLT to transform XML documents
into other formats, like XHTML.
CSS = Style Sheets for HTML
XSL = Style Sheets for XML
XSL can also be used to format data based on its
value; ex: to display negative numbers in a
financial report in red.
3

XSL consists of three parts




XSLT


XPATH


a language for transforming XML documents


into XHTML.

a language used by XSLT to locate elements


and/or attributes within an XML documents

XSL-FO


(Formatting Objects) a language for specifying


formatting properties for rendering the XML
4
documents

XSL


XSL can filter and sort XML data using the


criteria you define, as well as format its
display based on the value of the data
itself.

When the data is output, you can use XSL


to send your XML data to various devices,
including handheld devices, print, or voice
output.
5

XSLT Processing
reformatted.xsl
XSLT
Stylesheet

XSLT
Processor

reformatted.xml
(Reformatted XML
Document)

mydocument.xml
(XML
Document)

XSLT Processing


XSLT is used to transform XML Documents


with XSL.

Can transform XML to XHTML format to be


recognized by the browser, XSLT does this
by converting each XML element into an
XHTML element.

XSLT is a template-based programming


language.
7

XSLT Template


It can introduce new XML elements into the output


document it creates, or remove others.

Change the order of elements and choose which to display


and which to hide.

Create multiple views of the same source document to let


your data be displayed on a broad variety of devices.

Summary: XSLT lets different kinds of software applications


exchange XML-enabled data with one another.

XML Source Tree into XML Result Tree


<staffdirectory>
<employee>
<firstname>Lisa</firstname>
<lastname>Derik</lastname>
</employee>
<employee>
<firstname>Bill</firstname>
<lastname>Stark</lastname>
</employee>
</staffdirectory>

<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr>
<td>Lisa</td>
<td>Derik</td>
</tr>
<tr>
<td>Bill</td>
<td>Stark</td>
</tr>
</table>

Working


For XSLT transformation




Two documents required XML source document


and an XSLT stylesheet to create a single
result document.

XSLT uses


XPATH to define parts of the XML source


document that match one or more predefined
templates.

10

Details of XSL Stylesheet




Since an XSL is an XML document, it must begin with an XML


declaration:
<?xml version="1.0"?>

Every XSL stylesheet needs to identify the XSL namespace (xslns)


so that the parser knows what version of XSLT to use.

Hence the next statement defines a root element called


<xsl:stylesheet> or <xsl:tranform>.
<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">

11

Details of XSL Stylesheet




Later the prefix xsl: is used throughout


the rest of the XSL stylesheet to identify
all XSL processing statements.

If theres no xsl: at the beginning of a


bracketed tag, then that element is simply
copied to the output file without any
processing taking place. Thus with this we
can add HTML tags.
12

Example : catalog1.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"
href="xslcatalogs1.xsl"?>
<!DOCTYPE CATALOGS [
<!ELEMENT CATALOGS (BOOK)*>
<!ELEMENT BOOK (BOOKNAME, AUTHORNAME,
ISBN, PUBLISHER, PAGES?, PRICE)>
<!ELEMENT BOOKNAME (#PCDATA)>
<!ELEMENT AUTHORNAME (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT PUBLISHER (#PCDATA)>
<!ELEMENT PAGES (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>
]>
<CATALOGS>
<BOOK>
<BOOKNAME>Oracle 8i</BOOKNAME>
<AUTHORNAME>Richard
Gosling</AUTHORNAME>
<ISBN>0-07-913702-4</ISBN>
<PUBLISHER>BPB Publications</PUBLISHER>
<PAGES>393</PAGES>
<PRICE>410.00</PRICE>
</BOOK>

<BOOK>
<BOOKNAME>XML in Action</BOOKNAME>
<AUTHORNAME>William J.
Pardi</AUTHORNAME>
<ISBN>0-07-914702-9</ISBN>
<PUBLISHER>Microsoft Press</PUBLISHER>
<PAGES>492</PAGES>
<PRICE>470.00</PRICE>
</BOOK>
<BOOK>
<BOOKNAME>XML to code</BOOKNAME>
<AUTHORNAME>Jesse Liberty</AUTHORNAME>
<ISBN>1-861000-95-2</ISBN>
<PUBLISHER>Wrox Press</PUBLISHER>
<PAGES>393</PAGES>
<PRICE>560.00</PRICE>
</BOOK>
<BOOK>
<BOOKNAME>Java Unleashed</BOOKNAME>
<AUTHORNAME>James
Spencer</AUTHORNAME>
<ISBN>0-7456-0964-1</ISBN>
<PUBLISHER>Techmedia
Publications</PUBLISHER>
<PAGES>491</PAGES>
<PRICE>670.00</PRICE>
</BOOK>
13
</CATALOGS>

Example 1 : xslcatalogs1.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<center><h1 style="background:blue">Catalogs Book Information</h1></center>
<table align="center" border="1">
<tr style="font-weight:bold; font-size:18">
<td>BOOK NAME</td>
<td>AUTHOR NAME</td>
<td>ISBN</td>
<td>PUBLISHER</td>
<td>PAGES</td>
<td>PRICE</td>
</tr>
<xsl:for-each select=CATALOGS/BOOK">
<tr>
<td><xsl:value-of select="BOOKNAME"/></td>
<td><xsl:value-of select="AUTHORNAME"/></td>
<td><xsl:value-of select="ISBN"/></td>
<td><xsl:value-of select="PUBLISHER"/></td>
<td><xsl:value-of select="PAGES"/></td>
<td><xsl:value-of select="PRICE"/></td>
</tr>
</xsl:for-each> </table>
</body>
</html>
catalog1.xml
</xsl:template>
</xsl:stylesheet>

14

XSL uses Templates




The <xsl:template match=/> Element






The <xsl:template> element is used to build templates.


The match attribute indicates where to look for a match
between the XML and XSL documents.
The value of the match attribute is an XPath expression
(i.e. match="/" defines the whole document).

XSLT <xsl:value-of> Element





Is used to extract the value of a selected node.


<xsl:value-of select="BOOKNAME"/>

15

Filtering


XSLT <xsl:for-each> Element




The <xsl:for-each> element allows you to do looping in


XSLT

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=CATALOGS/BOOK[PAGES=393']">

Legal filter operators are:







= (equal)
!= (not equal)
&lt; (less than)
&gt; (greater than)
16

Sorting


XSLT <xsl:sort> Element


 The <xsl:sort> element is used to sort the
output.
 Ex : to sort the catalog as per book name in
alphabetical order
 <xsl:for-each select=CATALOGS/BOOK/>
 <xsl:sort select=BOOKNAME/>

17

Example 2 : xslcatalogs2.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<center><h1 style="background:blue">Catalogs Book Information</h1></center>
<table align="center" border="1">
<tr style="font-weight:bold; font-size:18">
<td>BOOK NAME</td>
<td>AUTHOR NAME</td>
<td>ISBN</td>
<td>PUBLISHER</td>
<td>PAGES</td>
<td>PRICE</td>
</tr>
<xsl:for-each select=CATALOGS/BOOK">
<xsl:sort select=BOOKNAME/>
<tr>
<td><xsl:value-of select="BOOKNAME"/></td>
<td><xsl:value-of select="AUTHORNAME"/></td>
<td><xsl:value-of select="ISBN"/></td>
<td><xsl:value-of select="PUBLISHER"/></td>
<td><xsl:value-of select="PAGES"/></td>
<td><xsl:value-of select="PRICE"/></td>
</tr>
</xsl:for-each> </table>
</body> </html>
catalog2.xml
</xsl:template>
</xsl:stylesheet>

18

Conditional Statement


The <xsl:if> element






Is used to put a conditional test attribute,


against the content of the XML file.
Ex: to display books only with price less than
500 rupees.
<xsl:if test=PRICE&lt;500>.</xsl:if>

19

Example 3 : xslcatalogs3.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<center><h1 style="background:blue">Catalogs Book Information</h1></center>
<table align="center" border="1">
<tr style="font-weight:bold; font-size:18">
<td>BOOK NAME</td>
<td>AUTHOR NAME</td>
<td>ISBN</td>
<td>PUBLISHER</td>
<td>PAGES</td>
<td>PRICE</td>
</tr>
<xsl:for-each select=CATALOGS/BOOK">
<xsl:if test=PRICE&lt;500>
<tr>
<td><xsl:value-of select="BOOKNAME"/></td>
<td><xsl:value-of select="AUTHORNAME"/></td>
<td><xsl:value-of select="ISBN"/></td>
<td><xsl:value-of select="PUBLISHER"/></td>
<td><xsl:value-of select="PAGES"/></td>
<td><xsl:value-of select="PRICE"/></td>
</tr>
</xsl:if>
</xsl:for-each> </table> </body> </html>
catalog3.xml
</xsl:template>
</xsl:stylesheet>

20

Multiple Choices


The <xsl:choose> element







Is used in conjunction with <xsl:when> and


<xsl:otherwise> to express multiple conditional tests.

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

21

Example 4.1 of choose(xml file)


<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="xchoose.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>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>HM 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>Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
<company>EMI</company>
<price>8.20</price>
<year>1987</year>
</cd>
</catalog>

22

If we want to display the cd details from the


XML file in a table and highlight the artist
where cd price is > 10 in a different color.

Example 4.1 of choose(xsl file)


<?xml version="1.0"?>
<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&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:stylesheet>

xchoose.xml

23

If we want to present the contents of our XML


file in a table and highlight the rows a different
color depending on the type of food it is.

Example 4.2 of choose(xml file)


<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href=foodchoose.xsl"?>
<food_list>
<food_item type="vegetable">
<name>Agar</name>
<carbs_per_serving>81</carbs_per_serving>
<fiber_per_serving>8</fiber_per_serving>
<fat_per_serving>0.5</fat_per_serving>
<kj_per_serving>1280</kj_per_serving>
</food_item>
<food_item type="vegetable">
<name>Asparagus</name>
<carbs_per_serving>1</carbs_per_serving>
<fiber_per_serving>1</fiber_per_serving>
<fat_per_serving>0</fat_per_serving>
<kj_per_serving>40</kj_per_serving>
</food_item>
<food_item type="vegetable">
<name>Cabbage</name>
<carbs_per_serving>0</carbs_per_serving>
<fiber_per_serving>1</fiber_per_serving>
<fat_per_serving>0</fat_per_serving>
<kj_per_serving>14</kj_per_serving>
</food_item>
<food_item type="vegetable">
<name>Potato</name>
<carbs_per_serving>21.5</carbs_per_serving>
<fiber_per_serving>2</fiber_per_serving>
<fat_per_serving>1</fat_per_serving>
<kj_per_serving>460</kj_per_serving>
</food_item>
:
:
</food_list>

24

To select attribute use @attribute_name.

Example 4.2 of choose(xml file)


<?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>

<td><xsl:value-of select="carbs_per_serving"/></td>
<td><xsl:value-of select="fiber_per_serving"/></td>
<td><xsl:value-of select="fat_per_serving"/></td>
<td><xsl:value-of select="kj_per_serving"/></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr style="background-color:#cccccc">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="carbs_per_serving"/></td>
<td><xsl:value-of select="fiber_per_serving"/></td>
<td><xsl:value-of select="fat_per_serving"/></td>
<td><xsl:value-of select="kj_per_serving"/></td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</xsl:template>

<xsl:template match="food_list">
<table>
<tr style="background-color:#ccff00">
<th>Food Item</th>
<th>Carbs (g)</th>
<th>Fiber (g)</th>
<th>Fat (g)</th>
<th>Energy (kj)</th>
</tr>
<xsl:for-each select="food_item">
<xsl:choose>
<xsl:when test="@type = 'grain'">
<tr style="background-color:#cccc00">
</xsl:stylesheet>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="carbs_per_serving"/></td>
<td><xsl:value-of select="fiber_per_serving"/></td>
<td><xsl:value-of select="fat_per_serving"/></td>
<td><xsl:value-of select="kj_per_serving"/></td>
</tr>
</xsl:when>
<xsl:when test="@type = 'vegetable'">
<tr style="background-color:#00cc00">
<td><xsl:value-of select="name"/></td>

food.xml

25

Applying Templates


The select attribute that appears in <xsl:apply-templates> element


processes only the child element that matches the value shown.
<xsl:template match=essay>
<h2><xsl:apply-templates select=subhead/></h2>
</xsl:template>
If the value is set to an asterisk, all child nodes of the current
element would be selected.
<xsl:template match=essay>
<h2><xsl:apply-templates select=*/></h2>
</xsl:template>
If value has to be set only to certain child elements then separate
with pipe character.
<xsl:template match=/>
<h2><xsl:apply-templates select=client/name |
client/address/></h2>
26
</xsl:template>

Example 5 : xsltemplate.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"
href="xsltemplate.xsl"?>
<school>
<student>
<roll>678</roll>
<name>
<first>Harsh</first>
<last>Mehta</last>
</name>
<course>
<year>BTech</year>
<trim>12</trim>
<branch>IT</branch>
</course>
</student>

<student>
<roll> 782 </roll>
<name>
<first>Smita</first>
<last>Londhe</last>
</name>
<course>
<year>MBATech</year>
<trim>12</trim>
<branch>Computer</branch>
</course>
</student>
</school>

27

Example 5 : xsltemplate.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
<xsl:template match="school">
<h2><xsl:apply-templates
select="student/name"/></h2>
</xsl:template>
</xsl:stylesheet>
xsltemplate.xml

28

Example 6 : xslplanes.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="xslplanes.xsl"?>
<planes>
<plane>
<year> 1977 </year>
<make> Cessna </make>
<model> Skyhawk </model>
<color> Light blue and white </color>
</plane>
<plane>
<year> 1975 </year>
<make> Piper </make>
<model> Apache </model>
<color> White </color>
</plane>
</planes>

29

Example 6 : xslplanes.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="planes">
<html>
<body>
<h2>Airplane Descriptions</h2>
<xsl:for-each select="plane">
<span style="font-style: italic"> Year: </span>
<xsl:value-of select="year" /><br />
<span style="font-style: italic"> Make: </span>
<xsl:value-of select="make" /><br />
<span style="font-style: italic"> Model: </span>
<xsl:value-of select="model" /><br />
<span style="font-style: italic"> Color: </span>
<xsl:value-of select="color" /><br /><br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

xslplanes.xml

30

XPath


XPath is used to navigate through


elements and attributes in an XML
document.

XPath is a major element in the W3C's


XSL standard. An understanding of XPath
is fundamental for advanced use of XML.

Without any XPath knowledge, you will not


be able to create XSLT documents.
31

XSL-FO


XSL-FO describes the formatting of XML


data for output to screen, paper or other
media.

XSL-FO documents are XML files with


information about the output layout and
output content.

32

Das könnte Ihnen auch gefallen