What Is XSL?: XSL Extensible Stylesheet Language
What Is XSL?: XSL Extensible Stylesheet Language
What Is XSL?: XSL Extensible Stylesheet Language
1
How does it work?
The XML source document is parsed into an XML
source tree
You use XPath to define templates that match parts of
the source tree
You use XSLT to transform the matched part and put
the transformed information into the result tree
The result tree is output as a result document
Parts of the source document that are not matched by a
template are typically copied unchanged
2
Simple XPath
Here’s a simple XML document: XPath expressions look
<?xml version="1.0"?> a lot like paths in a
<library> computer file system
<book>
<title>XML</title>
/ means the document
<author>Gregory Brill</author> itself (but no specific
</book> elements)
<book> /library selects the root
<title>Java and XML</title> element
<author>Brett McLaughlin</author> /library/book selects
</book> every book element
</library > //author selects every
author element,
wherever it occurs
3
Simple XSLT
<xsl:for-each select="//book"> loops through every
book element, everywhere in the document
<xsl:for-each select="//book">
<xsl:value-of select="title"/>
</xsl:for-each>
chooses the content of the title element for each book
in the XML document
4
Using XSL to create HTML
Our goal is to turn this: Into HTML that displays
<?xml version="1.0"?> something like this:
<library>
<book> Book Titles:
<title>XML</title>
<author>Gregory Brill</author>
• XML
</book> • Java and XML
<book> Book Authors:
<title>Java and XML</title> • Gregory Brill
<author>Brett McLaughlin</author> • Brett McLaughlin
</book>
</library >
Note that we’ve grouped
titles and authors
separately
5
What we need to do
We need to save our XML into a file (let’s call it
books.xml)
We need to create a file (say, books.xsl) that describes
how to select elements from books.xml and embed
them into an HTML page
We do this by intermixing the HTML and the XSL in the
books.xsl file
We need to add a line to our books.xml file to tell it to
refer to books.xsl for formatting information
6
books.xml, revised
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<library>
<book>
<title>XML</title> This tells you where
<author>Gregory Brill</author> to find the XSL file
</book>
<book>
<title>Java and XML</title>
<author>Brett McLaughlin</author>
</book>
</library >
7
Desired HTML
<html>
<head>
<title>Book Titles and Authors</title>
</head>
<body> Blue text is data extracted
<h2>Book titles:</h2> from the XML document
<ul>
<li>XML</li>
<li>Java and XML</li> Brown text is our
</ul>
<h2>Book authors:</h2> HTML template
<ul>
<li>Gregory Brill</li>
<li>Brett McLaughlin</li> We don’t necessarily
</ul> know how much data
</body>
</html> we will have
8
XSL outline
<?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="/">
</xsl:template>
</xsl:stylesheet>
9
Selecting titles and authors
<h2>Book titles:</h2>
<ul>
<xsl:for-each select="//book">
<li> Notice the
<xsl:value-of select="title"/> xsl:for-each
</li> loop
</xsl:for-each>
</ul>
<h2>Book authors:</h2>
...same thing, replacing title with author
Notice that XSL can rearrange the data; the HTML result
can present information in a different order than the XML
10
All of books.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<library>
<book>
<title>XML</title>
<author>Gregory Brill</author>
</book>
<book>
<title>Java and XML</title>
<author>Brett McLaughlin</author>
</book>
</library > Note: if you do View Source,
this is what you will see, not the
resultant HTML
11
All of books.xsl
<?xml version="1.0" encoding="ISO-8859-1"?> <h2>Book authors:</h2>
<xsl:stylesheet version="1.0" <ul>
xmlns:xsl="http://www.w3.org/1999/
<xsl:for-each
XSL/Transform">
<xsl:template match="/"> select="//book">
<html> <li>
<head> <xsl:value-of
<title>Book Titles and Authors</title> select="author"/>
</head> </li>
<body> </xsl:for-each>
<h2>Book titles:</h2>
<ul> </ul>
<xsl:for-each select="//book"> </body>
<li> </html>
<xsl:value-of select="title"/> </xsl:template>
</li> </xsl:stylesheet>
</xsl:for-each>
</ul>
12
How to use it
In a modern browser, such as Netscape 6, Internet
Explorer 6, or Mozilla 1.0, you can just open the
XML file
Older browsers will ignore the XSL and just show you
the XML contents as continuous text
You can use a program such as Xalan, MSXML,
or Saxon to create the HTML as a file
This can be done on the server side, so that all the client
side browser sees is plain HTML
The server can create the HTML dynamically from the
information currently in XML
13
The result (in IE)
14
The End
15