Credit: Jon A. Morse (ST Sci/NASA)
Properties of XSL
XSL (eXtensible Stylesheet Language) is a special purpose language
you use to convert XML into HTML and other XML, but which in the future
may
convert XML to many other formats, such as Braille, NC machine
instructions, control
instructions for robots, etc. XSL:
is entirely written in XML, which is the key
to understanding its unusual syntax. Recall that XML consists mostly
of elements and attributes, and consider the following
XSL statement:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
The above statement is composed of these XML components:
| <xsl:stylesheet |
the starting part of an element named xsl:stylesheet |
| xmlns:xsl |
the name of an attribute |
| = |
the equals sign for an attribute |
| "http://www.w3.org/1999/XSL/Transform" |
the value of the attribute |
| > |
the trailing bracket |
Similarly, this XSL statement:
<xsl:output method="html" indent="yes" />
contains these XML parts:
| <xsl:output |
an element named xsl:output |
| method= |
an attribute (with its "=" sign) |
| "html" |
the value of the 'method' attribute |
| indent= |
an attribute named 'indent' |
| "yes" |
the value of 'indent' |
and this statement:
<xsl:for-each select = "ancestor::PO/Attachments/attachment" >
contains the XML parts:
| <xsl:for-each |
an element named 'xsl:for-each' |
| select = |
an attribute named 'select' |
| "ancestor::PO/Attachments/attachment" |
the value of the attribute 'select' |
Executing XSL programs
There are several ways to execute XSL programs:
- You can apply XSL programs to XML files using the built in
capabilities of Internet Explorer versions 5 or above. You do so
by embedding the line:
<?xml-stylesheet type="text/xsl" href="my.xsl"?>
in your XML file where "my.xsl" is the name of your XSL file.
However, although it correctly displays the HTML output there
is no way that you can actually save the HTML file for further
editing, because if you View > Source
all you will see is the original XML data file. In addition,
Internet Explorer versions less than 6.0 only handle an older
version of XSL which
lacks many of the capabilities you need.
- You can use Xalan on the
www.washington.edu servers. It uses the newest
XSL version and also lets you save the HTML output file.
You execute XSL by invoking an XML file that begins with the
lines:
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="my.xsl"?>
and where the XSL program begins with the lines:
<?xml version="1.0" ?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" />
- You can also use Xalan on the Homer Web servers
through a CGI program. The following HTML (
xalan.html)
and CGI (xalan.cgi) programs convert XML to HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html><head><title>XML Converter</title></head>
<body>
<!-- Convert an XML file to HTML or XML using XSL -->
<form method="post" action="xalan.cgi">
XML file: <input type="text" name="xml"> <br>
XSL file: <input type="text" name="xsl"> <br>
<input type="submit" value="Convert XML">
</form>
</body></html>
The CGI program (xalan.cgi) is:
#!/usr/local/bin/perl
#
# Accepts an XML and an XSL file as inputs, and converts the XML to
# HTML using the XSL stylesheet. The XSL is processed by the Xalan
# XSLT processor which is available on the public_html web server
# through Homer.
#
# L. Gales, 12/17/2003
#
use CGI;
$query = new CGI;
$ENV{'LIBPATH'} = "/usr/local/xalan/lib"; # set paths for Xalan
$ENV{'PATH'} .= ":/usr/local/xalan";
my $xml = $query->param("xml"); chomp($xml); # Get the XML file
my $xsl = $query->param("xsl"); chomp($xsl); # Get the XSL file
print "Content-type: text/html\n\n";
system("/usr/local/xalan/bin/Xalan $xml $xsl" );
exit(0);
|
|
 |
|
 |