Book

Soit le schéma book.dtd si après.

1
<!ELEMENT book (part+) >
2
<!ELEMENT part (title, chapter+) >
3
<!ELEMENT chapter (title, para+) >
4
<!ELEMENT para (#PCDATA)>
5
<!ELEMENT title (#PCDATA)>

Question

Produisez un fichier book.xml valide permettant de tester une transformation XSLT.

Solution

1
<?xml version="1.0" encoding="UTF-8"?>
2
<book>
3
  <part>
4
    <title>First Part</title>
5
    <chapter>
6
      <title>Chapter Title</title>
7
       <para>Paragraphe</para>
8
    </chapter>
9
  </part>
10
</book>

Question

Proposez une feuille de transformation XSLT permettant la publication au format HTML.

Solution

1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
3
<xsl:template match="/book">
4
<html>
5
  <head>
6
    <title>A book</title>
7
  </head>
8
  <body>
9
    <xsl:apply-templates select="part"/>
10
  </body>
11
</html>
12
</xsl:template>
13
<xsl:template match="part">
14
  <xsl:apply-templates/>
15
</xsl:template>
16
<xsl:template match="chapter">
17
  <xsl:apply-templates/>
18
</xsl:template>
19
<xsl:template match="part/title">
20
  <h1><xsl:value-of select="."/></h1>
21
</xsl:template>
22
<xsl:template match="chapter/title">
23
  <h2><xsl:value-of select="."/></h2>
24
</xsl:template>
25
<xsl:template match="para">
26
  <p><xsl:value-of select="."/></p>
27
</xsl:template>
28
</xsl:stylesheet>

Question

Proposez une feuille de transformation XSLT permettant la publication au format FO.

Solution

1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet
3
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
4
  xmlns:fo="http://www.w3.org/1999/XSL/Format">
5
<xsl:template match="/book">
6
 <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
7
  <fo:layout-master-set>
8
    <fo:simple-page-master master-name="A4">
9
      <fo:region-body margin="3cm"/>
10
    </fo:simple-page-master>
11
  </fo:layout-master-set>
12
  <fo:page-sequence master-reference="A4">
13
    <fo:flow flow-name="xsl-region-body">
14
      <xsl:apply-templates/>
15
    </fo:flow>
16
  </fo:page-sequence>
17
</fo:root>
18
</xsl:template>
19
<xsl:template match="part | chapter">
20
  <xsl:apply-templates/>
21
</xsl:template>
22
<xsl:template match="part/title">
23
  <fo:block font-weight="bold" font-size="16pt"><xsl:value-of select="."/></fo:block>
24
</xsl:template>
25
<xsl:template match="chapter/title">
26
  <fo:block fo:inline font-weight="bold" font-size="14pt"><xsl:value-of select="."/></fo:block>
27
</xsl:template>
28
<xsl:template match="para">
29
  <fo:block><xsl:value-of select="."/></fo:block>
30
</xsl:template>
31
</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

1
<xsl:template match="/book">
2
<html>
3
  ...
4
  <body>
5
    <xsl:apply-templates select="part" mode="tdm"/>
6
    <hr/>
7
    <xsl:apply-templates select="part"/>
8
  </body>
9
</html>
10
</xsl:template>
11
<!-- TDM -->
12
<xsl:template match="part" mode="tdm">
13
  <xsl:apply-templates select="*" mode="tdm"/>
14
</xsl:template>
15
<xsl:template match="chapter" mode="tdm">
16
  <xsl:apply-templates select="title" mode="tdm"/>
17
</xsl:template>
18
<xsl:template match="part/title" mode="tdm">
19
  <p><b><xsl:value-of select="."/></b></p>
20
</xsl:template>
21
<xsl:template match="chapter/title" mode="tdm">
22
  <p><i><xsl:value-of select="."/></i></p>
23
</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

1
<xsl:template match="part">
2
  <xsl:apply-templates/>
3
  <ul>
4
    <li>Previous part : <xsl:value-of select="preceding::part[1]/title"/></li>
5
    <li>Next part : <xsl:value-of select="following::part[1]/title"/></li>
6
  </ul>
7
</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

1
<!--book0.xml-->
2
<?xml version="1.0" encoding="UTF-8"?>
3
<!DOCTYPE book SYSTEM "book.dtd">
4
<book>
5
<part ref="part1.xml"/>
6
<part ref="part2.xml"/>
7
</book>
1
<!--part1.xml-->
2
<?xml version="1.0" encoding="UTF-8"?>
3
<!DOCTYPE part SYSTEM "part.dtd">
4
<part>
5
<title>First Part</title>
6
<chapter>
7
<title>First Chapter</title>
8
<para>First paragraph</para>
9
<para>Second paragraph</para>
10
</chapter>
11
<chapter>
12
<title>Second Chapter</title>
13
<para>Paragraph</para>
14
</chapter>
15
</part>
1
<!--part2.xml-->
2
<?xml version="1.0" encoding="UTF-8"?>
3
<!DOCTYPE part SYSTEM "part.dtd">
4
<part>
5
<title>Second Part</title>
6
<chapter>
7
<title>Chapter</title>
8
<para>Paragraph</para>
9
</chapter>
10
</part>

Question

Proposer la DTD book2.dtd correspondant au fichier book.xml.

Solution

1
<!-- book.dtd -->
2
<!ELEMENT book (part+) >
3
<!ELEMENT part EMPTY>
4
<!ATTLIST part ref CDATA #REQUIRED>

Question

Proposer la DTD part.dtd correspondant aux fichiers parti.xml.

Solution

1
<!-- part.dtd -->
2
<!ELEMENT part (title, chapter+) >
3
<!ELEMENT chapter (title, para+) >
4
<!ELEMENT para (#PCDATA)>
5
<!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

1
<xsl:template match="/book">
2
<html>
3
...
4
  <body>
5
    <xsl:apply-templates select="document(part/@ref)/part" mode="tdm"/>
6
    <hr/>
7
    <xsl:apply-templates select="document(part/@ref)/part"/>
8
  </body>
9
</html>
10
</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

1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
3
  <xsl:output method="xml"/>
4
  <xsl:template match="book">
5
    <book>
6
      <xsl:copy-of select="document(part/@ref)/part"/>
7
    </book>
8
  </xsl:template>
9
</xsl:stylesheet>