CS835 - Data and Document Representation
& Processing
|
Lecture 5 - Authoring |
From:
http://nwalsh.com/docs/tutorials/xsl/xsl/slides.html
·
XML is not a fixed tag
set (like HTML)
·
XML by itself has no
(application) semantics
·
A generic XML processor
has no idea what is "meant" by the XML
·
XML markup does not (usually) include formatting information
·
The information in an
XML document may not be in the form in which it is desired to present it
·
Need something in
addition to the XML document that provides information on how to present or
otherwise process the XML
·
Separation of style
from content allows for the same data to be presented in different ways.
·
Enabling:
o
Reuse of fragments of
data: the same content should look different in different contexts
o
Multiple output
formats: different media (paper, online), different sizes (manuals, reports),
different classes of output devices (workstations, hand-held devices)
o
Styles tailored to the
reader's preference (e.g., accessibility): print size, color, simplified layout
for audio readers
o
Standardized styles:
corporate stylesheets can be applied to the content at any time
o
Freedom from style
issues for content authors: technical writers needn't be concerned with layout
issues because the correct style can be applied later
·
A stylesheet specifies
the presentation of XML information using two basic categories of techniques:
·
An optional
transformation of the input document into another structure
·
A description of how to
present the transformed information (i.e., a specification of what properties
to associate to each of the various parts of the transformed information)
·
Transformation
capabilities include:
o
generation of constant
text
o
suppression of content
o
moving text (e.g.,
exchanging the order of the first and last name)
o
duplicating text (e.g.,
copying titles to make a table of contents)
o
sorting
o
more complex
transformations that "compute" new information in terms of the
existing information
·
Description of how to
present the (possibly transformed) data includes three levels of formatting
information:
·
Specification of the
general screen or page (or even audio) layout
·
Assignment of the
transformed content into basic "content container types" (e.g.,
lists, paragraphs, inline text)
·
Specification of
formatting properties (spacing, margins, alignment, fonts, etc.) for each
resulting "container"
·
The full XSL language
logically consists of three component languages which are described in three
W3C (World Wide Web Consortium) Recommendations:
o
XPath: XML Path
Language--a language for referencing specific parts of an XML document
o
XSLT: XSL
Transformations--a language for describing how to transform one XML document
(represented as a tree) into another
o
XSL: Extensible
Stylesheet Language--XSLT plus a description of a set of Formatting Objects and
Formatting Properties
An XSLT
"stylesheet" transforms the input (source) document's tree into a
structure called a result tree
consisting of result objects
·
The result tree's
structure is that of an XML document, and its objects correspond to elements
with attributes
·
The result tree's
structure and "tag set" can match that of any XML document or
doctype.
o
In particular, the
result tree could be:
§
HTML/XHTML
result
tree is easily written out as an HTML document
§
other
XML doctype
result tree is easily written out as an XML document
in this other doctype (for some further application-specific processing)
§
FO
result tree
result
tree's structure (and element and attribute names) matches the set of formatting objects and formatting properties defined by the (non-transformation)
part of XSL
·
Serialization of the
result tree is not necessary for further processing of the result tree.
·
An XSL stylesheet
basically consists of a set of templates
·
Each template
"matches" some set of elements in the source tree and then describes
the contribution that the matched element makes to the result tree
·
Generally, elements in
a stylesheet in the "xsl" namespace are part of the XSLT language,
and non-xsl elements within a template are what get put into the result tree
·
Transformation is
independent of the target result type
·
Most people are more familiar
with HTML so many of the examples in this tutorial use HTML
·
The XSL implementation
in IE5 is incomplete. The examples in this tutorial will not work in IE5
·
The techniques apply
equally well to XSL Formatting Objects or other tag sets
·
XSLT is a tree-to-tree
transformation process
·
Serialization may vary
depending on the selected output method
·
There is a distinction
between HTML element names and HTML
·
XSLT Stylesheets are
XML documents; namespaces
(http://www.w3.org/TR/REC-xml-names) are used to identify semantically
significant elements.
·
Most stylesheets are
stand-alone documents rooted at <xsl:stylesheet> or
<xsl:transform>. It is possible to have "single template"
stylesheet/documents.
·
<xsl:stylesheet>
and <xsl:transform> are completely synonymous.
Note that it is the mapping
from namespace abbreviation to URI that is important, not the literal namespace
abbreviation "xsl:" that is used most commonly.
A Stylesheet
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> ... </xsl:stylesheet> |
A Transformation Sheet
<eg:transform
xmlns:eg="http://www.w3.org/1999/XSL/Transform" version="1.0"> ... </eg:transform> |
Document as Stylesheet
<html
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <head> <title>Silly Example</title> </head> <body> <h1>Silly Example</h1> <p>You'd probably use extension elements, or somthing more interesting in real life: 3+4 is <xsl:value-of
select="3+4"/>. </p> </body> </html> |
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> |
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> |
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 XSLT compliant browser it will nicely transform your XML into XHTML!
View
the result in IE 6 or Netscape 6 and
· An XSL style sheet consists of a set of rules called templates.
· Each <xsl:template> element contains rules to apply when a specified node is matched.
· The <xsl:template> element contains rules to apply when a specified node is matched.
· The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).
· The following XSL Style Sheet contains a template to output the XML CD Catalog
<?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>Title</th> <th>Artist</th> </tr> <tr> <td>.</td> <td>.</td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
· Since the style sheet is an XML document itself, the document begins with an xml declaration: <?xml version="1.0" encoding="ISO-8859-1"?>.
· The <xsl:stylesheet> tag defines the start of the style sheet.
· The <xsl:template> tag defines the start of a template. The match="/" attribute associates (matches) the template to the root (/) of the XML source document.
· The rest of the document contains the template itself, except for the last two lines that defines the end of the template and the end of the style sheet.
· The result of the transformation will look (a little disappointing) like this:
My CD Collection
Title |
Artist |
. |
. |
· If you have Netscape 6 or IE 5 or higher you can view: the XML file, the XSL file, and the result
· The result from this example was a little disappointing, because no data was copied from the XML document to the output.
<xsl:value-of> element can be used to select the value of an XML element and add it to the output stream of the transformation:
<?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>Title</th> <th>Artist</th> </tr> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
Note: The value of the required select attribute contains an XPath expression. It works like navigating a file system where a forward slash (/) selects subdirectories.
The result of the transformation will look like this:
Title |
Artist |
Empire Burlesque |
Bob Dylan |
If you have Netscape 6 or IE 5 or higher you can view the XML file and the XSL file
View the result in IE 6 or Netscape 6 and 7
· Only one line of data was copied from the XML document to the output.
· Next <xsl:for-each> will be used to select the values of several XML elements, and add them to the output.
· The <xsl:for-each> element allows you to do looping in XSLT.
· The XSL <xsl:for-each> element can be used to select every XML element of a specified node set:
<?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>Title</th> <th>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> |
·
Note: The value of the required select
attribute contains an XPath expression.
· It works like navigating a file system where a forward slash (/) selects subdirectories.
· The result of the transformation will look like this:
Title |
Artist |
Empire Burlesque |
Bob Dylan |
Hide your heart |
Bonnie Tyler |
Greatest Hits |
Dolly Parton |
Still got the blues |
Gary More |
Eros |
Eros Ramazzotti |
One night only |
Bee Gees |
Sylvias Mother |
Dr.Hook |
Maggie May |
Rod Stewart |
Romanza |
Andrea Bocelli |
When a man loves a woman |
Percy Sledge |
Black angel |
Savage Rose |
1999 Grammy Nominees |
Many |
For the good times |
Kenny Rogers |
Big Willie style |
Will Smith |
Tupelo Honey |
Van Morrison |
Soulsville |
Jorn Hoel |
The very best of |
Cat Stevens |
Stop |
Sam Brown |
Bridge of Spies |
T`Pau |
Private Dancer |
Tina Turner |
Midt om natten |
Kim Larsen |
Pavarotti Gala Concert |
Luciano Pavarotti |
The dock of the bay |
Otis Redding |
Picture book |
Simply Red |
Red |
The Communards |
Unchain my heart |
Joe Cocker |
· View: the XML file and the XSL file
· View the result with Netscape 6 or IE 6
· We can filter the output from an XML file by adding a criterion to the select attribute in the <xsl:for-each> element.
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
Legal filter operators are:
Take a look at the adjusted XSL style sheet:
<?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>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> <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> |
· The result of the transformation will look like this:
Title |
Artist |
Empire Burlesque |
Bob Dylan |
· View: the XML file and the XSL file.
· View the result with Netscape 6 or IE 6
To output the XML file as an XHTML file, and sort it at the same time, simply add a sort element inside the for-each element in your XSL file:
<?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>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <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> |
The select attribute indicates what XML element to sort on.
The result of the transformation will look like this:
Title |
Artist |
Romanza |
Andrea Bocelli |
One night only |
Bee Gees |
Empire Burlesque |
Bob Dylan |
Hide your heart |
Bonnie Tyler |
The very best of |
Cat Stevens |
Greatest Hits |
Dolly Parton |
Sylvias Mother |
Dr.Hook |
Eros |
Eros Ramazzotti |
Still got the blues |
Gary Moore |
Unchain my heart |
Joe Cocker |
Soulsville |
Jorn Hoel |
For the good times |
Kenny Rogers |
Midt om natten |
Kim Larsen |
Pavarotti Gala Concert |
Luciano Pavarotti |
1999 Grammy Nominees |
Many |
The dock of the bay |
Otis Redding |
When a man loves a woman |
Percy Sledge |
Maggie May |
Rod Stewart |
Stop |
Sam Brown |
Black angel |
Savage Rose |
Picture book |
Simply Red |
Bridge of Spies |
T`Pau |
Red |
The Communards |
Private Dancer |
Tina Turner |
Tupelo Honey |
Van Morrison |
Big Willie style |
Will Smith |
· View: the XML file and the XSL file.
· View the result with Netscape 6 or IE 6
Note: Cannot view the result in IE 5, because the "http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:sort> element.
· The <xsl:if> element contains a template that will be applied only if a specified condition is true.
· To put a conditional if test against the content of the file, simply add an <xsl:if> element to your XSL document like this:
<xsl:if test="price > 10">
some output ...
</xsl:if>
· The value of the required test attribute contains the expression to be evaluated.
· Take a look at the adjusted XSL style sheet:
<?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>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
· The code above only selects the title and artist IF the price of the cd is higher than 10.
· The result of the transformation will look like this:
Title |
Artist |
Empire Burlesque |
Bob Dylan |
Still got the blues |
Gary Moore |
One night only |
Bee Gees |
Romanza |
Andrea Bocelli |
Black Angel |
Savage Rose |
1999 Grammy Nominees |
Many |
· View: the XML file and the XSL file.
· View the result with Netscape 6 or IE 6
Note: Unable to view the result in IE 5, because the "http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:if> element.
· The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.
· To insert a conditional choose test against the content of the XML file, simply add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to your XSL document like this:
<xsl:choose>
<xsl:when test="price > 10">
... some code ...
</xsl:when>
<xsl:otherwise>
... some code ....
</xsl:otherwise>
</xsl:choose>
Look at the adjusted XSL style sheet:
<?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>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 > 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> |
· The code above will add a pink background-color to the artist column WHEN the price of the cd is higher than 10.
· The result of the transformation will look like this:
Title |
Artist |
Empire Burlesque |
Bob Dylan |
Hide your heart |
Bonnie Tyler |
Greatest Hits |
Dolly Parton |
Still got the blues |
Gary Moore |
Eros |
Eros Ramazzotti |
One night only |
Bee Gees |
Sylvias Mother |
Dr.Hook |
Maggie May |
Rod Stewart |
Romanza |
Andrea Bocelli |
When a man loves a woman |
Percy Sledge |
Black angel |
Savage Rose |
1999 Grammy Nominees |
Many |
For the good times |
Kenny Rogers |
Big Willie style |
Will Smith |
Tupelo Honey |
Van Morrison |
Soulsville |
Jorn Hoel |
The very best of |
Cat Stevens |
Stop |
Sam Brown |
Bridge of Spies |
T`Pau |
Private Dancer |
Tina Turner |
Midt om natten |
Kim Larsen |
Pavarotti Gala Concert |
Luciano Pavarotti |
The dock of the bay |
Otis Redding |
Picture book |
Simply Red |
Red |
The Communards |
Unchain my heart |
Joe Cocker |
· View: the XML file and the XSL file.
· View the result with Netscape 6 or IE 6
· Note: Unable to view the result in IE 5, because the "http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:choose> element.
Here is another example that contains several <xsl:when> elements:
<?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>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 > 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:when test="price > 9 and price <= 10"> <td bgcolor="#cccccc"> <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> |
· The code above will add a pink background color to the artist column WHEN the price of the cd is higher than 10, and a grey background-color WHEN the price of the cd is higher than 9 and lower or equal to 10.
· The result of the transformation will look like this:
Title |
Artist |
Empire Burlesque |
Bob Dylan |
Hide your heart |
Bonnie Tyler |
Greatest Hits |
Dolly Parton |
Still got the blues |
Gary Moore |
Eros |
Eros Ramazzotti |
One night only |
Bee Gees |
Sylvias Mother |
Dr.Hook |
Maggie May |
Rod Stewart |
Romanza |
Andrea Bocelli |
When a man loves a woman |
Percy Sledge |
Black angel |
Savage Rose |
1999 Grammy Nominees |
Many |
For the good times |
Kenny Rogers |
Big Willie style |
Will Smith |
Tupelo Honey |
Van Morrison |
Soulsville |
Jorn Hoel |
The very best of |
Cat Stevens |
Stop |
Sam Brown |
Bridge of Spies |
T`Pau |
Private Dancer |
Tina Turner |
Midt om natten |
Kim Larsen |
Pavarotti Gala Concert |
Luciano Pavarotti |
The dock of the bay |
Otis Redding |
Picture book |
Simply Red |
Red |
The Communards |
Unchain my heart |
Joe Cocker |
· View: the XML file and the XSL file.
· View the result with Netscape 6 or IE 6
Note: Unable to view the result in IE 5, because the "http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:choose> element.
· The <xsl:apply-templates> element applies a template rule to the current element or to the current element's child nodes.
· If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.
· Look at the following XSL style sheet:
<?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> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /> </xsl:template> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /> </xsl:template> </xsl:stylesheet> |
· The result of the transformation will look like this:
Title: Empire
Burlesque
Artist: Bob Dylan
Title: Hide
your heart
Artist: Bonnie Tyler
Title: Greatest
Hits
Artist: Dolly Parton
Title: Still
got the blues
Artist: Gary Moore
Title: Eros
Artist: Eros Ramazzotti
Title: One
night only
Artist: Bee Gees
Title: Sylvias
Mother
Artist: Dr.Hook
Title: Maggie
May
Artist: Rod Stewart
Title: Romanza
Artist: Andrea Bocelli
Title: When
a man loves a woman
Artist: Percy Sledge
Title: Black
angel
Artist: Savage Rose
Title: 1999
Grammy Nominees
Artist: Many
Title: For the
good times
Artist: Kenny Rogers
Title: Big
Willie style
Artist: Will Smith
Title: Tupelo
Honey
Artist: Van Morrison
Title: Soulsville
Artist: Jorn Hoel
Title: The
very best of
Artist: Cat Stevens
Title: Stop
Artist: Sam Brown
Title: Bridge
of Spies
Artist: T`Pau
Title: Private
Dancer
Artist: Tina Turner
Title: Midt
om natten
Artist: Kim Larsen
Title: Pavarotti
Gala Concert
Artist: Luciano Pavarotti
Title: The
dock of the bay
Artist: Otis Redding
Title: Picture
book
Artist: Simply Red
Title: Red
Artist: The Communards
Title: Unchain
my heart
Artist: Joe Cocker
· View: the XML file and the XSL file.
· View the result with Netscape 6 or IE 6
Note: Unable to view the result in IE 5, because the "http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:apply-template> element.
· If your browser supports it, XSLT can be used to transform the document to XHTML in your browser.
· Not always desirable to include a style sheet reference in an XML file (i.e. it will not work in a non XSLT aware browser.)
· A more versatile solution would be to use a JavaScript to do the XML to XHTML transformation.
· By using JavaScript, we can:
· That's the beauty of XSLT. One of the design goals for XSLT was to make it possible to transform data from one format to another, supporting different browsers and different user needs.
· XSLT transformation on the client side is bound to be a major part of the browsers work tasks in the future, as we will see a growth in the specialized browser market (Braille, aural browsers, Web printers, handheld devices, etc.)
Take a new look at the XML document
<?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> |
· If you have Netscape 6 or IE 5 or higher you can view the XML file.
· And the accompanying XSL style sheet:
<?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 the XSL file.
· Note: Be sure to notice that the XML file does not have a reference to the XSL file.
· IMPORTANT: The above sentence indicates that an XML file could be transformed using many different XSL files.
Here is the source code needed to transform the XML file to XHTML on the client:
<html> <body> <script type="text/javascript"> // Load XML var xml = new ActiveXObject("Microsoft.XMLDOM") xml.async = false xml.load("cdcatalog.xml") // Load XSL var xsl = new ActiveXObject("Microsoft.XMLDOM") xsl.async = false xsl.load("cdcatalog.xsl") // Transform document.write(xml.transformNode(xsl)) </script> </body> </html> |
· The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML document into memory.
· The second block of code creates another instance of the parser and loads the XSL document into memory. The last line of code transforms the XML document using the XSL document, and writes the result to the XHTML document. Nice!
If you have IE 6.0: See how it works.
If you have IE 5.0: See how it works.
· Since not all browsers support XSLT, one solution is to transform the XML to XHTML on the server.
· To make XML data available to all kinds of browsers, we have to transform the XML document on the SERVER and send it as pure XHTML to the BROWSER.
· That's another beauty of XSLT. One of the design goals for XSLT was to make it possible to transform data from one format to another on a server, returning readable data to all kinds of future browsers.
· XSLT transformation on the server is bound to be a major part of the Internet Information Server work tasks in the future, as we will see a growth in the specialized browser market (Braille, aural browsers, Web printers, handheld devices, etc.)
new look at the XML document:
<?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> |
· View the XML file.
· And the accompanying XSL style sheet:
<?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> |
If you have Netscape 6 or IE 5 or higher you can view the XSL file.
Note: Be sure that the XML file does not have a reference to the XSL file.
IMPORTANT: The above sentence indicates that an XML file on the server could be transformed using many different XSL files.
Here is the source code needed to transform the XML file to XHTML on the server:
<% 'Load XML set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = false xml.load(Server.MapPath("cdcatalog.xml")) 'Load XSL set xsl = Server.CreateObject("Microsoft.XMLDOM") xsl.async = false xsl.load(Server.MapPath("cdcatalog.xsl")) 'Transform file Response.Write(xml.transformNode(xsl)) %> |
· The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML file into memory.
· The second block of code creates another instance of the parser and loads the XSL document into memory.
· The last line of code transforms the XML document using the XSL document, and returns the result to the browser. Nice!
The links in the "Element" columns point to attributes and more useful information about the specific element.
Note: All elements supported in IE 5.X may have NON-standard behavior, because IE 5.X was released before XSLT became a W3C Recommendation!
Element |
Description |
IE |
NN |
Applies a template rule from an imported style sheet |
6.0 |
|
|
Applies a template rule to the current element or to the
current element's child nodes |
5.0 |
6.0 |
|
Adds an attribute |
5.0 |
6.0 |
|
Defines a named set of attributes |
6.0 |
6.0 |
|
Calls a named template |
6.0 |
6.0 |
|
Used in conjunction with <when> and
<otherwise> to express multiple conditional tests |
5.0 |
6.0 |
|
Creates a comment node in the result tree |
5.0 |
6.0 |
|
Creates a copy of the current node (without child nodes
and attributes) |
5.0 |
6.0 |
|
Creates a copy of the current node (with child nodes and
attributes) |
6.0 |
6.0 |
|
Defines the characters and symbols to be used when
converting numbers into strings, with the format-number() function |
6.0 |
|
|
Creates an element node in the output document |
5.0 |
6.0 |
|
Specifies an alternate code to run if the processor
does not support an XSLT element |
6.0 |
|
|
Loops through each node in a specified node set |
5.0 |
6.0 |
|
Contains a template that will be applied only if a
specified condition is true |
5.0 |
6.0 |
|
Imports the contents of one style sheet into another. Note:
An imported style sheet has lower precedence than the importing style sheet |
6.0 |
6.0 |
|
Includes the contents of one style sheet into another. Note:
An included style sheet has the same precedence as the including style sheet |
6.0 |
6.0 |
|
Declares a named key that can be used in the style sheet
with the key() function |
6.0 |
6.0 |
|
Writes a message to the output (used to report errors) |
6.0 |
6.0 |
|
Replaces a namespace in the style sheet to a different
namespace in the output |
6.0 |
|
|
Determines the integer position of the current node and
formats a number |
6.0 |
6.0 |
|
Specifies a default action for the <choose> element |
5.0 |
6.0 |
|
Defines the format of the output document |
6.0 |
6.0 |
|
Declares a local or global parameter |
6.0 |
6.0 |
|
Defines the elements for which white space should be
preserved |
6.0 |
6.0 |
|
Writes a processing instruction to the output |
5.0 |
6.0 |
|
Sorts the output |
6.0 |
6.0 |
|
Defines the elements for which white space should be
removed |
6.0 |
6.0 |
|
Defines the root element of a style sheet |
5.0 |
6.0 |
|
Rules to apply when a specified node is matched |
5.0 |
6.0 |
|
Writes literal text to the output |
5.0 |
6.0 |
|
Defines the root element of a style sheet |
6.0 |
6.0 |
|
Extracts the value of a selected node |
5.0 |
6.0 |
|
Declares a local or global variable |
6.0 |
6.0 |
|
Specifies an action for the <choose> element |
5.0 |
6.0 |
|
Defines the value of a parameter to be passed into a
template |
6.0 |
6.0 |
Name |
Description |
Returns the current node |
|
Used to access the nodes in an external XML document |
|
Tests whether the element specified is supported by the
XSLT processor |
|
Converts a number into a string |
|
Tests whether the function specified is supported by the
XSLT processor |
|
Returns a string value that uniquely identifies a
specified node |
|
Returns a node-set using the index specified by an
<xsl:key> element |
|
Returns the value of the system properties |
|
Returns the URI of an unparsed entity |
|
· XSL-FO is about formatting XML data for output.
XSL-FO is an XML based markup language describing the formatting of XML data for output to screen, paper or other media.
· XSL-FO and XSL the same thing?
· Styling is both about transforming and formatting information
· When the World Wide Web Consortium (W3C) made their first XSL Working Draft, it contained the language syntax for both transforming and formatting XML documents.
· Later the XSL Working Group at W3C split the original draft into separate Recommendations:
§ XSLT, a language for transforming information
§ XSL or XSL-FO, a language for formatting information
§ XPath, a language for defining parts of an XML document
· XSL-FO became a W3C Recommendation 15. October 2001. Formally named XSL.
·
To read more about the XSL activities at W3C please
read our W3C Tutorial.
· XSL-FO documents are XML files with output information.
· XSL-FO documents are XML files with output information. They contain information about the output layout and output contents.
· XSL-FO documents are stored in files with a *.fo or a *.fob extension. It is also quite normal to see XSL-FO documents stored with the *.xml extension, because this makes them more accessible to XML editors.
XSL-FO documents have a structure like this:
<?xml version="1.0" encoding="ISO-8859-1"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="A4"> <!-- Page template goes here --> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4"> <!-- Page content goes here --> </fo:page-sequence> </fo:root> |
· XSL-FO documents are XML documents, and must always start with an XML declaration:
<?xml version="1.0" encoding="ISO-8859-1"?> |
· The <fo:root> element contains the XSL-FO document. It also declares the namespace for XSL-FO:
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <!-- The full XSL-FO document goes here --> </fo:root> |
· The <fo:layout-master-set> element contains one or more page templates:
<fo:layout-master-set> <!-- All page templates go here --> </fo:layout-master-set> |
· Each <fo:simple-page-master> element contains a single page template. Each template must have a unique name (master-name):
<fo:simple-page-master master-name="A4"> <!-- One page template goes here --> </fo:simple-page-master> |
· One or more <fo:page-sequence> elements describe page contents. The master-reference attribute refers to the simple-page-master template with the same name:
<fo:page-sequence master-reference="A4"> <!-- Page content goes here --> </fo:page-sequence> |
Note: The master-reference "A4" does not actually describe a predefined page format. It is just a name. You can use any name like "MyPage", "MyTemplate", etc.
· XSL-FO uses rectangular boxes (areas) to display output.
· The XSL formatting model defines a number of rectangular areas (boxes) to display output.
· All output (text, pictures, or whatever) will be formatted into these boxes and then displayed or printed to a target media.
· They include:
§ XSL-FO output is formatted into pages.
§ Printed output will normally go into many separate pages.
§ Browser output will often go into one long page.
§ XSL-FO Pages contain Regions.
Each XSL-FO Page contains a number of Regions:
· XSL-FO Regions contain Block areas.
· XSL-FO Block areas define small block elements (the ones that normally starts with a new line) like paragraphs, tables and lists.
· XSL-FO Block areas can contain other Block areas, but most often they contain Line areas.
· XSL-FO Line areas define text lines inside Block areas.
· XSL-FO Line areas contain Inline areas.
· XSL-FO Inline areas define text inside Lines (bullets, single character, graphics, and more).
· XSL-FO defines output inside <fo:flow> elements.
· "Blocks" of content "Flows" into "Pages" and then to the output media.
· XSL-FO output is normally nested inside <fo:block> elements, nested inside <fo:flow> elements, nested inside <fo:page-sequence> elements:
<fo:page-sequence> <fo:flow flow-name="xsl-region-body"> <fo:block> <!-- Output goes here --> </fo:block> </fo:flow> </fo:page-sequence> |
XSL-FO example:
<?xml version="1.0" encoding="ISO-8859-1"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="A4"> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4"> <fo:flow flow-name="xsl-region-body"> <fo:block>Hello W3Schools</fo:block> </fo:flow> </fo:page-sequence> </fo:root> |
· The output from this code would be something like this:
Hello W3Schools
|
· XSL-FO pages are filled with data from <fo:flow> elements.
· XSL-FO uses <fo:page-sequence> elements to define output pages.
o Each output page refers to a page master which defines the layout.
o Each output page has a <fo:flow> element defining the output.
o Each output page is printed (or displayed) in sequence.
· XSL-FO pages are filled with content from the <fo:flow> element.
· The <fo:flow> element contains all the elements to be printed to the page.
· When the page is full, the same page master will be used over (and over) again until all the text is printed.
· The <fo:flow> element has a "flow-name" attribute.
· The value of the flow-name attribute defines where the content of the <fo:flow> element will go.
· The legal values are:
· XSL-FO uses page templates called "Page Masters" to define the layout of pages.
· XSL-FO uses page templates called "Page Masters" to define the layout of pages. Each template must have a unique name:
<fo:simple-page-master master-name="intro"> <fo:region-body margin="5in" /> </fo:simple-page-master> <fo:simple-page-master master-name="left"> <fo:region-body margin-left="2in" margin-right="3in" /> </fo:simple-page-master> <fo:simple-page-master master-name="right"> <fo:region-body margin-left="3in" margin-right="2in" /> </fo:simple-page-master> |
In the example above:
· Three <fo:simple-page-master> elements, define three different templates.
· Each template (page-master) has a different name.
· The first template is called "intro". It could be used as a template for introduction pages.
· The second and third templates are called "left" and "right".
§ They could be used as templates for even and odd page numbers.
XSL-FO uses the following attributes to define the size of a page:
XSL-FO uses the following attributes to define the margins of a page:
XSL-FO uses the following elements to define the regions of a page:
· Note that the region-before, region-after, region-start, and region-end is a part of the body region.
· To avoid text in the body region to overwrite text in these regions, the body region must have margins at least the size of these regions.
|
This is an extract from an XSL-FO document:
<fo:simple-page-master master-name="A4" page-width="297mm" page-height="210mm" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm"> <fo:region-body margin="3cm"/> <fo:region-before extent="2cm"/> <fo:region-after extent="2cm"/> <fo:region-start extent="2cm"/> <fo:region-end extent="2cm"/> </fo:simple-page-master> |
· The code above defines a "Simple Page Master Template" with the name "A4".
· The width of the page is 297 millimeters and the height is 210 millimeters.
· The top, bottom, left, and right margins of the page are all 1 centimeter.
· The body has a 3 centimeter margin (on all sides).
· The before, after, start, and end regions (of the body) are all 2 centimeters.
· The width of the body in the example above can be calculated by subtracting the left and right margins and the region-body margins from the width of the page itself:
297mm - (2 x 1cm) - (2 x 3cm) = 297mm - 20mm - 60mm = 217mm.
· Note that the regions (region-start and region-end) are not a part of the calculation. As described earlier, these regions are parts of the body.
· XSL-FO output goes into blocks.
· "Blocks" of content "Flow" into "Pages" of the output media.
· XSL-FO output is normally:
o nested inside <fo:block> elements,
o nested inside <fo:flow> elements
o nested inside <fo:page-sequence> elements:
<fo:page-sequence> <fo:flow flow-name="xsl-region-body"> <fo:block> <!-- Output goes here --> </fo:block> </fo:flow> </fo:page-sequence> |
Blocks are sequences of output in rectangular boxes:
<fo:block border-width="1mm"> This block of output will have a one millimeter border around it. </fo:block> |
Since block areas are rectangular boxes, they
share many common area properties:
space before |
margin
|
space after |
· The space before and space after is the empty space separating the block from the other blocks.
· The margin is the empty area on the outside of the block.
· The border is the rectangle drawn around the external edge of the area. It can have different widths on all four sides. It can also be filled with different colors and background images.
· The padding is the area between the border and the content area.
· The content area contains the actual content like text, pictures, graphics, or whatever.
Border style attributes:
Border color attributes:
Border width attributes:
Blocks are sequences of output that can be styled individually:
<fo:block font-size="12pt" font-family="sans-serif"> This block of output will be written in a 12pt sans-serif font. </fo:block> |
Font attributes:
Text attributes:
<fo:block font-size="14pt" font-family="verdana" font-color="red" space-before="5mm" space-after="5mm"> W3Schools </fo:block> <fo:block text-indent="5mm" font-family="verdana" font-size="12pt" space-before="5mm" space-after="5mm"> At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP. </fo:block> |
Result:
At W3Schools you will find
all the Web-building tutorials you need, from basic HTML and XHTML to
advanced XML, XSL, Multimedia and WAP. |
· Takes a lot of code to produce a document with many headers and paragraphs.
· Normally XSL-FO document do not combine formatting information and content as was have done here.
· XSLT can put the formatting information into templates and write a cleaner content.
· XSL-FO uses List Blocks to define lists.
There are four XSL-FO objects used to create lists:
An XSL-FO list example:
<fo:list-block> <fo:list-item> <fo:list-item-label> <fo:block>*</fo:block> </fo:list-item-label> <fo:list-item-body> <fo:block>Volvo</fo:block> </fo:list-item-body> </fo:list-item> <fo:list-item> <fo:list-item-label> <fo:block>*</fo:block> </fo:list-item-label> <fo:list-item-body> <fo:block>Saab</fo:block> </fo:list-item-body> </fo:list-item> </fo:list-block> |
The output from this code would be:
|
· XSL-FO uses the <fo:table-and-caption> element to define tables.
The XSL-FO table model is not very different from the HTML table model.
There are nine XSL-FO objects used to create tables:
· XSL-FO uses the <fo:table-and-caption> element to define a table. It contains a <fo:table> and an optional <fo:caption> element.
· The <fo:table> element contains:
o optional <fo:table-column> elements
o an optional <fo:table-header> element
o a <fo:table-body> element
o an optional <fo:table-footer> element
· Each of these elements has one or more <fo:table-row> elements, with one or more <fo:table-cell> elements:
<fo:table-and-caption> <fo:table> <fo:table-column column-width="25mm"/> <fo:table-column column-width="25mm"/> <fo:table-header> <fo:table-cell> <fo:block font-weight="bold">Car</fo:block> </fo:table-cell> <fo:table-cell> <fo:block font-weight="bold">Price</fo:block> </fo:table-cell> </fo:table-header> <fo:table-body> <fo:table-row> <fo:table-cell> <fo:block>Volvo</fo:block> </fo:table-cell> <fo:table-cell> <fo:block>$50000</fo:block> </fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell> <fo:block>SAAB</fo:block> </fo:table-cell> <fo:table-cell> <fo:block>$48000</fo:block> </fo:table-cell> </fo:table-row> </fo:table-body> </fo:table> </fo:table-and-caption> |
The output from this code would something like this:
Car |
Price |
Volvo |
$50000 |
SAAB |
$48000 |
· XSL-FO and XSLT can help each other.
<fo:block font-size="14pt" font-family="verdana" font-color="red" space-before="5mm" space-after="5mm"> W3Schools </fo:block> <fo:block text-indent="5mm" font-family="verdana" font-size="12pt" space-before="5mm" space-after="5mm"> At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP. </fo:block> |
Result:
At W3Schools you will find
all the Web-building tutorials you need, from basic HTML and XHTML to
advanced XML, XSL, Multimedia and WAP. |
Remove the XSL-FO information from the document:
<header> W3Schools </header> <paragraph> At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP. </paragraph> |
Add an XSLT transformation:
<xsl:template match="header"> <fo:block font-size="14pt" font-family="verdana" font-color="red" space-before="5mm" space-after="5mm"> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="paragraph"> <fo:block text-indent="5mm" font-family="verdana" font-size="12pt" space-before="5mm" space-after="5mm"> <xsl:apply-templates/> </fo:block> </xsl:template> |
And the result will be the same:
At W3Schools you will find
all the Web-building tutorials you need, from basic HTML and XHTML to
advanced XML, XSL, Multimedia and WAP. |
· XSL-FO needs formatting software to produce output.
An XSL-FO processor is a software program for formatting XSL documents for output.
Most XSL-FO processors can output PDF document, and quality print as well as HTML and other formats.
Here is a list of the most common XSL-FO processors:
XSL Formatter is a software to format XML documents for production-quality printing and output to PDF.
Antenna House has been providing version V2 of the same product since January, 2002 in the global market, and XSL Formatter was rated as one of the best quality product at the XML 2002, XML 2003 conferences held in Europe.
Building on over 4 years of experience developing XSL-FO software, Antenna House has completely written from scratch an entirely new Formatter that offers significant enhancements and provides a solid foundation on which to continue to move forward.
Xinc is an XSL-FO processor by Lunasil LTD.
Xinc is designed to be fast, multithreaded and memory efficient. A Swing based XSL-FO viewer allows you to view and print XSL-FO files as well as generate PDF files with the click of a button. Xinc can be used as a server component via its Java API. Xinc can also be used in a Microsoft server environment by using its COM interface. New features include hyphenation, basic-link, PDF output, memory/speed optimizations and a simple COM interface.
Inventive Designers Scriptura is a cross-platform document design and generation solution based on XSLT and XSL-FO.
Scriptura has a WYSIWYG design tool and engine. The XSL-FO formatter used in the engine is no longer based on Apache FOP, but is written from scratch by Inventive Designers. The new features in this release are: support for bulleted and numbered lists, 'break-before' and 'break-after' properties, extended bar code options and improved number and currency formatting. A free trial version is available for download.
The process that converts a description into a presentation is called formatting.
Object |
Description |
Represents the start resource of a link |
|
Overrides the default Unicode BIDI direction |
|
Defines a block of output (e.g. paragraphs and titles) |
|
Defines a block-level reference-area |
|
Specifies a character that will be mapped to a glyph for
presentation |
|
Defines a color-profile for a stylesheet |
|
Specifies a page-master to be used when the conditions
defined are true |
|
Groups global declarations for a stylesheet |
|
Used for a graphic where the graphics data resides outside
of the XML result tree |
|
Typically used to position an image in a separate area at
the beginning of a page OR to position an image to one side, with the content
flowing along-side of the image |
|
Contains all elements to be printed to a page |
|
Defines a footnote within the region-body of a page |
|
Defines the content of the footnote |
|
Formats the first line of an <fo:block> |
|
Formats a part of a text with a background or enclosing it
in a border |
|
Defines an inline reference-area |
|
Used for inline graphics or for "generic"
objects where the object's data resides as descendants of
<fo:instream-foreign-object> |
|
Holds all masters used in a document |
|
Used to generate "."
to separate titles from page numbers in table of contents, or to create input
fields in forms, or to create horizontal rules |
|
Defines a list |
|
Contains each item in the list |
|
Contains the content/body of the list-item |
|
Contains the label for the list-item (typically a number,
character, etc.) |
|
Used with <fo:retrieve-marker> to create running
headers or footers |
|
Contains (within an
<fo:multi-switch>) each alternative sub-tree of XSL-FO objects. The
parent <fo:multi-switch> will choose which alternative to show and hide
the rest |
|
Used to switch between two or more property-sets |
|
Specifies an alternative property-set that will be applied
depending on the state of the user agent |
|
Holds one or more <fo:multi-case> objects and
controls the switching between them (activated by <fo:multi-toggle>) |
|
Used to switch to another <fo:multi-case> |
|
Represents the current page-number |
|
References the page-number for the page that contains the
first normal area returned by the cited object |
|
A container for page output elements. There will be one
<fo:page-sequence> object for each page layout |
|
Specifies which simple-page-masters are to be used and in
which order |
|
Defines a page footer |
|
Defines a page header |
|
Defines a page body |
|
Defines the right sidebar of a page |
|
Defines the left sidebar of a page |
|
Specifies repetition of a set of simple-page-masters |
|
Specifies repetition of a single simple-page-master |
|
Used with <fo:marker> to create running headers or
footers |
|
The root (top) node for XSL-FO documents |
|
Defines the size and shape of a page |
|
Specifies a page-master to be used at a given point in the
sequence of pages |
|
Contains static content (e.g. headers and footers) that
will be repeated on many pages |
|
Formats the tabular material of a table |
|
Formats a table and its caption |
|
Container for table rows and table cells |
|
Contains the caption for a table |
|
Defines a table cell |
|
Formats the columns of a table |
|
Defines a table footer |
|
Defines a table header |
|
Defines a table row |
|
Defines a title for a page-sequence |
|
Specifies inherited properties for a group of XSL-FO
objects |
|