Technologies XML / XSLT / Recettes XSLT / Transformer XML en HTML / XSLT comme langage de mise en forme
(Extrait de « XSLT Kochbuch » de Sal Mangano)
Problème
Vous souhaitez que le navigateur transforme dynamiquement un document XML en HTML en lui appliquant un style.
Solution
Une solution XSLT 1.0 suffit. Cet exemple publie en HTML une partie d’un document DocBook avec une feuille XSLT. La source est une partie de ce chapitre :
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="chapter.xsl"?>
<chapter label="8">
<chapterinfo>
<author>
<surname>Mangano</surname>
<firstname>Sal</firstname>
</author>
<copyright>
<year>2002</year>
<holder>O'Reilly</holder>
</copyright>
</chapterinfo>
<title>XML in HTML transformieren</title>
<epigraph>
<para>Mich hat es überrascht, dass die Leute klaglos bereit waren, umständlich HTML einzugeben.</para>
<attribution>Tim Berners-Lee</attribution>
</epigraph>
<sect1>
<title>XSLT als Styling-Sprache verwenden</title>
<sect2>
<title>Problem</title>
<para>Sie möchten, dass der Browser dynamisch einen Stil auf ein XML-Dokument anwendet, um daraus HTML zu machen.</para>
</sect2>
<sect2>
<title>Lösung</title>
<para>Es wird lediglich eine XSLT 1.0-Lösung benötigt. Es folgt ein Beispiel für die Veröffentlichung eines Teilaussschnitts eines DocBook-Dokuments in HTML, bei der ein XSLT-Stylesheet verwendet wird. Als Quelldokument dient ein Teil dieses Kapitels.</para>
</sect2>
<sect2>
<title>Diskussion</title>
<para>DocBook ist eine dokumentorientierte DTD, mit der Sie Dokumentinhalte erstellen und in einer präsentationsunabhängigen Form speichern können, in der die logische Struktur des Inhalts festgehalten wird. Das Schöne beim Erstellen von Dokumenten (besonders bei technischen Dokumenten) in dieser Form ist, dass Sie mit XSLT einen einzigen Inhalt in mehrere verschiedene Ausgabeformate wie HTML, PDF, Microsoft-Hilfsdateien und Unix-Manpages transformieren können. Auch wenn es bei diesem Buchbeispiel konkret um DocBook geht, lassen sich die gleichen Techniken ebenso gut bei anderen Dokumentschemata verwenden, seien es welche, die als Public-Domain verfügbar sind, oder solche, die Sie sich selbst ausgedacht haben.</para>
</sect2>
</sect1>
</chapter>La deuxième ligne contient une instruction de traitement, à savoir xml-stylesheet. Elle indique au navigateur d’appliquer cette feuille au XML et d’afficher le résultat plutôt que le XML. Cela ne fonctionne que dans les versions récentes des navigateurs :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<xsl:apply-templates mode="head"/>
</head>
<!-- Vielleicht möchten Sie lieber Styles in einem CSS-Style-Element verwenden, als sie fest zu kodieren, wie ich es hier mache -->
<body style="margin-left: 100; margin-right: 100; margin-top: 50; margin-bottom: 50">
<xsl:apply-templates/>
<xsl:apply-templates select="chapter/chapterinfo/*" mode="copyright"/>
</body>
</html>
</xsl:template>
<!-- Head -->
<xsl:template match="chapter" mode="head">
<xsl:apply-templates select="chapterinfo" mode="head" />
<xsl:apply-templates select="title" mode="head" />
</xsl:template>
<xsl:template match="chapter/title" mode="head">
<title>
<xsl:value-of select="."/>
</title>
</xsl:template>
<xsl:template match="author" mode="head">
<meta name="author" content="{concat(firstname,' ', surname)}"/>
</xsl:template>
<xsl:template match="copyright" mode="head">
<meta name="copyright" content="{concat(holder,' ',year)}"/>
</xsl:template>
<xsl:template match="text( )" mode="head"/>
<!-- Body -->
<xsl:template match="chapter">
<div align="right" style="font-size: 48pt; font-family: Times serif; font-weight: bold; padding-bottom: 10; color: red">
<xsl:value-of select="@label"/>
</div>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="chapter/title">
<div align="right" style="font-size: 24pt; font-family: Times serif; padding-bottom: 150; color:red">
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:template match="epigraph/para">
<div align="right" style="font-size: 10pt; font-family: Times serif; font-style: italic; padding-top: 4; padding-bottom: 4">
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:template match="epigraph/attribution">
<div align="right" style="font-size: 10pt; font-family: Times serif; padding-top: 4; padding-bottom: 4">
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:template match="sect1">
<h1 style="font-size: 18pt; font-family: Times serif; font-weight: bold">
<xsl:value-of select="title"/>
</h1>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="sect2">
<h2 style="font-size: 14pt; font-family: Times serif; font-weight: bold">
<xsl:value-of select="title"/>
</h2>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="para">
<p style="font-size: 12pt; font-family: Times serif">
<xsl:value-of select="."/>
</p>
</xsl:template>
<xsl:template match="text( )"/>
<xsl:template match="copyright" mode="copyright">
<div style="font-size: 10pt; font-family: Times serif; padding-top: 100">
<xsl:text>Copyright </xsl:text>
<xsl:value-of select="holder"/>
<xsl:text> </xsl:text>
<xsl:value-of select="year"/>
<xsl:text>. All rights reserved.</xsl:text>
</div>
</xsl:template>
<xsl:template match="*" mode="copyright"/>
</xsl:stylesheet>Le navigateur reçoit finalement ce HTML :
<html>
<head>
<meta name="author" content="Sal Mangano">
<meta name="copyright" content="O'Reilly 2002">
<title>XML in HTML transformieren</title>
</head>
<body style="margin-left:100;margin-right:100;margin-top:50;margin-bottom:50">
<div align="right" style="font-size : 48pt; font-family: Times serif; font-weight : bold; padding-bottom:10; color:red">8</div>
<div align="right" style="font-size : 24pt; font-family: Times serif; padding-bottom:150; color:red">XML to HTML</div>
<div align="right" style="font-size : 10pt; font-family: Times serif; font-style : italic; padding-top:4; padding-bottom:4">Mich hat es überrascht, dass die Leute klaglos bereit waren, umständlich HTML einzugeben.</div>
<div align="right" style="font-size : 10pt; font-family: Times serif; padding-top:4; padding-bottom:4">Tim Berners-Lee</div>
<h1 style="font-size : 18pt; font-family: Times serif; font-weight : bold">XSLT als Styling-Sprache verwenden</h1>
<h2 style="font-size : 14pt; font-family: Times serif; font-weight : bold">Problem</h2>
<p style="font-size : 12pt; font-family: Times serif">Sie möchten, dass der Browser dynamisch einen Stil auf ein XML-Dokument anwendet, um daraus HTML zu machen.</p>
<h2 style="font-size : 14pt; font-family: Times serif; font-weight : bold">Lösung</h2>
<p style="font-size : 12pt; font-family: Times serif">Es wird lediglich eine XSLT 1.0-Lösung benötigt. Es folgt ein Beispiel für die Veröffentlichung eines Teilausschnitts eines DocBook-Dokuments in HTML, bei der ein XSLT-Stylesheet verwendet wird. Die Quelle dieses Dokuments ist Teil dieses Kapitels:</p>
<h2 style="font-size : 14pt; font-family: Times serif; font-weight : bold">Discussion</h2>
<p style="font-size : 12pt; font-family: Times serif">DocBook ist eine dokumentorientierte DTD, mit der Sie Dokumentinhalte erstellen und in einer präsentationsunabhängigen Form speichern können, in der die logische Struktur des Inhalts festgehalten wird. Das Schöne beim Erstellen von Dokumenten (besonders bei technischen Dokumenten) in dieser Form ist, dass Sie mit XSLT einen einzigen Inhalt in mehrere verschiedene Ausgabeformate wie HTML, PDF, Microsoft-Hilfsdateien und Unix-Manpages transformieren können. Auch wenn es bei diesem Buchbeispiel konkret um DocBook geht, lassen sich die gleichen Techniken ebenso gut bei anderen Dokumentschemata verwenden, seien es welche, die als Public-Domain verfügbar sind, oder solche, die Sie sich selbst ausgedacht haben.</p>
<div style="font-size : 10pt; font-family: Times serif; padding-top : 100">Copyright O'Reilly 2002. All rights reserved.</div>
</body>
</html>Discussion
DocBook est une DTD documentaire permettant de rédiger et stocker des contenus indépendamment de leur présentation tout en préservant leur structure logique. C’est particulièrement utile pour la documentation technique : XSLT transforme une source en HTML, PDF, aide Microsoft ou pages de manuel Unix. Ces techniques valent aussi pour d’autres schémas publics ou personnalisés.
L’utilisation d’un sous-ensemble de DocBook rendait pratique une feuille monolithique. Une solution de production répartirait les éléments entre feuilles et modèles distincts utilisant des modes.
Un inconvénient est le style codé en dur. Les navigateurs actuels prenant généralement en charge Cascading Stylesheets (CSS), mieux vaut confier la transformation à XSLT et le style à CSS.
La structure de base reste, mais remplacez les attributs codés en dur style par des attributs class référençant une feuille nommée style.css, ajoutée à l’élément HTML head:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<xsl:apply-templates mode="head"/>
<link href="style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<xsl:apply-templates/>
<xsl:apply-templates select="chapter/chapterinfo/*" mode="copyright"/>
</body>
</html>
</xsl:template>
<!-- Head -->
<xsl:template match="chapter" mode="head">
<xsl:apply-templates select="chapterinfo" mode="head" />
<xsl:apply-templates select="title" mode="head" />
</xsl:template>
<xsl:template match="chapter/title" mode="head">
<title>
<xsl:value-of select="."/>
</title>
</xsl:template>
<xsl:template match="author" mode="head">
<meta name="author" content="{concat(firstname,' ', surname)}"/>
</xsl:template>
<xsl:template match="copyright" mode="head">
<meta name="copyright" content="{concat(holder,' ',year)}"/>
</xsl:template>
<xsl:template match="text( )" mode="head"/>
<!-- Body -->
<xsl:template match="chapter">
<div class="chapter">
<xsl:value-of select="@label"/>
</div>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="chapter/title">
<div class="title">
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:template match="epigraph/para">
<div class="epigraph">
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:template match="epigraph/attribution">
<div class="epigraph-attribution">
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:template match="sect1">
<h1>
<xsl:value-of select="title"/>
</h1>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="sect2">
<h2>
<xsl:value-of select="title"/>
</h2>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="para">
<p class="para">
<xsl:value-of select="."/>
</p>
</xsl:template>
<xsl:template match="text( )"/>
<xsl:template match="copyright" mode="copyright">
<div class="copyright">
<xsl:text>Copyright </xsl:text>
<xsl:value-of select="holder"/>
<xsl:text> </xsl:text>
<xsl:value-of select="year"/>
<xsl:text>. All rights reserved.</xsl:text>
</div>
</xsl:template>
<xsl:template match="*" mode="copyright"/>
</xsl:stylesheet>Un CSS est simplement un fichier ASCII (style.css) suivant quelques conventions simples :
body
{
margin-left:100;
margin-right:100;
margin-top:50;
margin-bottom:50;
font-family: Times serif;
font-size : 12pt;
color:black;
}
div.chapter
{
text-align:right;
font-size : 48pt;
font-weight: bold;
padding-bottom:10;
color:red;
}
div.title
{
font-size : 24pt;
font-family: Times serif;
padding-bottom:150;
color:red"
}
div.epigraph
{
font-style:: italic;
}
div.epigraph, div.epigraph-attribution
{
text-align: right;
font-size : 10pt;
padding-top: 4;
padding-bottom: 4;
}
h1
{
font-size : 18pt;
font-weight : bold;
}
h2
{
font-size : 14pt;
font-weight : bold";
}Pour découvrir CSS, une bonne introduction se trouve sur W3Schools. Je recommande aussi Cascading Style Sheets – Das umfassende Handbuch d’Eric Meyer (O’Reilly, 2004). De nombreux outils créent du CSS. J’apprécie Macromedia Dreamweaver MX, peut-être surdimensionné pour un simple éditeur CSS.
Voir aussi
La meilleure source sur DocBook est Docbook.org. Norman Walsh a développé des feuilles libres convertissant DocBook en de nombreux formats de publication. Elles sont sur DocBookXSLStylesheets.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
Vous pouvez imprimer la version en ligne pour votre usage privé.
Pour tout autre usage, ce chapitre de « XSLT Kochbuch » est soumis aux mêmes conditions que l’édition reliée. L’ouvrage et toutes ses parties sont protégés par le droit d’auteur. Tous droits réservés, notamment reproduction, traduction, microfilmage, stockage et traitement dans des systèmes électroniques.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de