Please visit our sponsors !
XSL - Transformation (XSLT)
Example study: How to transform XML into XHTML using XSL.
The details of this example will be explained in the next
chapter.
Correct Style Sheet Declaration
The root element that declares the document to be an XSL style sheet is <xsl:stylesheet>
or <xsl:transform>.
Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous
and either can be used!
The correct way to declare an XSL style sheet according to the W3C XSL Recommendation
is:
<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">
|
Note: The xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
identifies the official W3C XSL recommendation namespace. If you use this
namespace, you must also include the attribute version="1.0".
Note: If you are using IE 6.0 or Netscape 6 you should use one of the codes
above.
Incorrect Style Sheet Declaration
This was the correct way to declare an XSL style sheet according to the
W3C XSL
Working Draft:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/TR/WD-xsl">
|
Note: The declaration above is OUTDATED, but if you are using IE 5 you should use the (incorrect) code
above.
Start with your XML Document
We want to transform the following XML document ("cdcatalog.xml") into
XHTML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
|
To view an XML / XSL document in IE 5.0 (and higher) you can click on a link,
type the URL in the address bar, or double-click on the name of an XML file in a
files folder.
To view an XML / XSL document in Netscape 6 you'll have to open the XML file
and then right-click in XML file and select "View Page Source".
View XML file
Create an XSL Style Sheet
Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:
<?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 align="left">Title</th>
<th align="left">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>
|
View XSL file
Link the XSL Style Sheet to the XML Document
Finally, add an 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>
|
If you have an XSL compliant browser it will nicely transform your XML
into XHTML!
View the result in IE 6
or Netscape 6
View the result in IE
5
Example Explained
The details of the example above will be explained in the next chapters!
Jump to: Top of Page
or HOME or
Printer friendly page
Search W3Schools:
What Others Say About Us
Does the world know about us? Check out these places:
Dogpile
Alta Vista
MSN
Google
Excite
Lycos
Yahoo
Ask Jeeves
We Help You For Free. You Can Help Us!
W3Schools is for training only. We do not warrant its correctness or its fitness for use.
The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our
terms of use and
privacy policy.
Copyright 1999-2002 by Refsnes Data. All Rights Reserved
|