Book
Soit le schéma book.dtd
si après.
<!ELEMENT book (part+) >
<!ELEMENT part (title, chapter+) >
<!ELEMENT chapter (title, para+) >
<!ELEMENT para (#PCDATA)>
<!ELEMENT title (#PCDATA)>
Question
Produisez un fichier book.xml
valide permettant de tester une transformation XSLT.
Solution
<book>
<part>
<title>First Part</title>
<chapter>
<title>Chapter Title</title>
<para>Paragraphe</para>
</chapter>
</part>
</book>
Question
Proposez une feuille de transformation XSLT permettant la publication au format HTML.
Solution
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/book">
<html>
<head>
<title>A book</title>
</head>
<body>
<xsl:apply-templates select="part"/>
</body>
</html>
</xsl:template>
<xsl:template match="part">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="chapter">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="part/title">
<h1><xsl:value-of select="."/></h1>
</xsl:template>
<xsl:template match="chapter/title">
<h2><xsl:value-of select="."/></h2>
</xsl:template>
<xsl:template match="para">
<p><xsl:value-of select="."/></p>
</xsl:template>
</xsl:stylesheet>
Question
Proposez une feuille de transformation XSLT permettant la publication au format FO.
Solution
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/book">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="A4">
<fo:region-body margin="3cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates/>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="part | chapter">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="part/title">
<fo:block font-weight="bold" font-size="16pt"><xsl:value-of select="."/></fo:block>
</xsl:template>
<xsl:template match="chapter/title">
<fo:block fo:inline font-weight="bold" font-size="14pt"><xsl:value-of select="."/></fo:block>
</xsl:template>
<xsl:template match="para">
<fo:block><xsl:value-of select="."/></fo:block>
</xsl:template>
</xsl:stylesheet>
Question
Intégrez une table des matières au format HTML (sans hyperliens).
(ne réécrire que les templates modifiés ou ajoutés)
Solution
<xsl:template match="/book">
<html>
...
<body>
<xsl:apply-templates select="part" mode="tdm"/>
<hr/>
<xsl:apply-templates select="part"/>
</body>
</html>
</xsl:template>
<!-- TDM -->
<xsl:template match="part" mode="tdm">
<xsl:apply-templates select="*" mode="tdm"/>
</xsl:template>
<xsl:template match="chapter" mode="tdm">
<xsl:apply-templates select="title" mode="tdm"/>
</xsl:template>
<xsl:template match="part/title" mode="tdm">
<p><b><xsl:value-of select="."/></b></p>
</xsl:template>
<xsl:template match="chapter/title" mode="tdm">
<p><i><xsl:value-of select="."/></i></p>
</xsl:template>
Question
Ajoutez une liste avec le titre de la part
précédente et le titre de la part
suivante à la fin de chaque part
(après le dernier chapitre qui la compose donc), pour le format HTML seulement.
(ne réécrire que les templates modifiés ou ajoutés)
Solution
<xsl:template match="part">
<xsl:apply-templates/>
<ul>
<li>Previous part : <xsl:value-of select="preceding::part[1]/title"/></li>
<li>Next part : <xsl:value-of select="following::part[1]/title"/></li>
</ul>
</xsl:template>
Réutilisation par transclusion
On souhaite à présent pouvoir réutiliser des fragments part
entre plusieurs documents, sans les recopier (en utilisant les mécanismes de transclusion).
Question
Proposez des fichiers XML correspondant à votre exemple de test, permettant de gérer les parties comme des fichiers externes réutilisables par transclusion. Le fichier book0.xml
sera la racine et les fichiers parti.xml
les parties référencées.
Solution
<!--book0.xml-->
<book>
<part ref="part1.xml"/>
<part ref="part2.xml"/>
</book>
<!--part1.xml-->
<part>
<title>First Part</title>
<chapter>
<title>First Chapter</title>
<para>First paragraph</para>
<para>Second paragraph</para>
</chapter>
<chapter>
<title>Second Chapter</title>
<para>Paragraph</para>
</chapter>
</part>
<!--part2.xml-->
<part>
<title>Second Part</title>
<chapter>
<title>Chapter</title>
<para>Paragraph</para>
</chapter>
</part>
Question
Proposer la DTD book2.dtd
correspondant au fichier book.xml
.
Solution
<!-- book.dtd -->
<!ELEMENT book (part+) >
<!ELEMENT part EMPTY>
<!ATTLIST part ref CDATA #REQUIRED>
Question
Proposer la DTD part.dtd
correspondant aux fichiers parti.xml
.
Solution
<!-- part.dtd -->
<!ELEMENT part (title, chapter+) >
<!ELEMENT chapter (title, para+) >
<!ELEMENT para (#PCDATA)>
<!ELEMENT title (#PCDATA)>
Question
Proposez une nouvelle feuille de transformation XSLT adaptée à vos nouveaux schémas.
(ne réécrire que les templates modifiés ou ajoutés)
Solution
<xsl:template match="/book">
<html>
...
<body>
<xsl:apply-templates select="document(part/@ref)/part" mode="tdm"/>
<hr/>
<xsl:apply-templates select="document(part/@ref)/part"/>
</body>
</html>
</xsl:template>
Question
Proposez un programme XSLT permettant de créer un fichier XML valide par rapport à book.dtd
, à partir d'un fichier valide par rapport à book2.dtd
et des fichiers valides par rapport à part.dtd
référencés.
Solution
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:template match="book">
<book>
<xsl:copy-of select="document(part/@ref)/part"/>
</book>
</xsl:template>
</xsl:stylesheet>