xsltproc
tool.Let's start with a simple example
A more complex example (HTML generation)
<?xml version="1.0"?>
<article>
<title>The Pecan pie recipe</title>
<ingredients>Eggs, Sugar, Pecan nuts</ingredients>
<body>
Scramble the eggs, add the sugar and the pecan nuts.
Cook in the oven for 30 minutes.
<br/>
</body>
</article>
|
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="article"> --- <xsl:value-of select="title"/> --- Ingredients: ------------ <xsl:value-of select="ingredients"/> Procedure: ---------- <xsl:apply-templates select="body"/> ********************************************** </xsl:template> </xsl:stylesheet> |
![]() |
![]() |
shell> | |
![]() | |
<?xml version="1.0"?>
--- The Pecan pie recipe ---
Ingredients:
------------
Eggs, Sugar, Pecan nuts
Procedure:
----------
Scramble the eggs, add the sugar and the pecan nuts.
Cook in the oven for 30 minutes.
**********************************************
|
<xsl:template match="...">
instruction.<article>
tag and then applies the XSLT rule described in <xsl:template match="article">
on the <article>...</article>
part.<article>...</article>
part, the XSLT processor continues with what follows (i.e. nothing in our example).<xsl:apply-templates select="..."/>
instruction.
<?xml version="1.0"?>
<main>
<article>
<title>The Pecan pie recipe</title>
<ingredients>Eggs, Sugar, Pecan nuts</ingredients>
<body>
Scramble the eggs, add the sugar and the pecan nuts.<br/>
Cook in the oven for 30 minutes.<br/>
(<a href="http://x.y.com/">click here</a>)
</body>
</article>
<article>
<title>The scramble eggs recipe</title>
<ingredients>Eggs</ingredients>
<body>
Scramble the eggs.<br/>
Cook in the pan for 4 minutes.<br/>
</body>
</article>
</main>
|
<?xml version="1.0" encoding="ISO-8859-15"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output encoding="ISO-8859-15"/> <xsl:template match="br"> <br/> </xsl:template> <xsl:template match="a"> <a href="{@href}"><xsl:apply-templates/></a> </xsl:template> <xsl:template match="article"> <h3><xsl:value-of select="title"/></h3> <u>Ingredients:</u> <xsl:value-of select="ingredients"/> <br/> <u>Procedure:</u> <xsl:apply-templates select="body"/> </xsl:template> <xsl:template match="main"> <html> <head> <title>recipes</title> </head> <body bgcolor="#ffffff"> <h2>Our best recipes "à la française"</h2> <xsl:apply-templates select="article"/> </body> </html> </xsl:template> </xsl:stylesheet> |
![]() |
![]() |
shell> | |
![]() | |
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15"> <title>recipes</title> </head> <body bgcolor="#ffffff"> <h2>Our best recipes "à la française"</h2> <h3>The Pecan pie recipe</h3> <u>Ingredients:</u>Eggs, Sugar, Pecan nuts<br><u>Procedure:</u> Scramble the eggs, add the sugar and the pecan nuts.<br> Cook in the oven for 30 minutes.<br> (<a href="http://x.y.com/">click here</a>) <h3>The scramble eggs recipe</h3> <u>Ingredients:</u>Eggs<br><u>Procedure:</u> Scramble the eggs.<br> Cook in the pan for 4 minutes.<br> <body><html> |
encoding="ISO-8859-15"
enables the use of accented characters (àçéâöù...).<br/>
is necessary to have a valid XML file.xsltproc
does special processing if it recognizes that the output file format is HTML (for example it adds the <meta ... content="text/html ... ">
).<b>...</b>
had been present in data2.xml
(to indicate some bold HTML), then both tags <b>
and </b>
would have been removed by xsltproc
(look at the next paragraph to learn how to preserve our <b>
and </b>
). This also explains why we have the <xsl:template match="br">
part in our example: we want to preserve the <br>
tag.name()
function<xsl:element name="{name()}">
to quote HTML tags:
<h2>TITLE OF THE PARAGRAPH</h2> <u>notes:</u> one<br/> two<br/> |
<xsl:template match="ul|li|b|u|i"> <xsl:element name="{name()}"> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="br"> <xsl:element name="{name()}"/> </xsl:template> |
![]() |
![]() |
shell> | |
![]() | |
TITLE OF THE PARAGRAPH <u>notes:</u> one<br/> two<br/> |
<h2>...</h2>
tags have been removed becaused they are not handled by the XSL file.
<xsl:copy>
instruction
<any_tag value="322"/> <another_tag> Mister Rabbit goes to the swimming pool. </another_tag> |
<xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> |
![]() |
![]() |
shell> | |
![]() | |
<any_tag/> <another_tag> Mister Rabbit goes to the swimming pool. </another_tag> |
... |
<xsl:comment>This file is automatically generated. Do not edit! </xsl:comment> |
![]() |
![]() |
shell> | |
![]() | |
<!--This file is automatically generated. Do not edit! --> |
-stringparam
option of xsltproc
... |
<input type="hidden" name="date" value="{$date}"/> |
![]() |
![]() |
shell> | |
![]() | |
<input type="hidden" name="date" value="2005-01-31"/> |
{$date}
is not used inside a tag <x ... y="{$date}" ...>
.