XSLFO n THEORIE n PRATIQUE THEORIE XSLFO Prsentation

  • Slides: 25
Download presentation
XSL-FO n THEORIE n PRATIQUE

XSL-FO n THEORIE n PRATIQUE

THEORIE XSL-FO

THEORIE XSL-FO

Présentation n XSL-FO (XSL Formatting Objects) permet d’exprimer de manière très précise le rendu

Présentation n XSL-FO (XSL Formatting Objects) permet d’exprimer de manière très précise le rendu d’un document. Il gère la pagination d’un document, les notes de bas de page, les marges, … Il est possible de préciser avec exactitude l’emplacement des différents objets sur la page, les polices des caractères, l’affichage de tableaux, etc. n Un document XSL-FO est assez pénible à réaliser à la main, c’est pourquoi on utilise XSLT dans le but de générer du XSL-FO à partir de données au format XML.

Présentation n Ce langage, qui a failli tomber dans les oubliettes, doit sa renaissance

Présentation n Ce langage, qui a failli tomber dans les oubliettes, doit sa renaissance à FOP (Formatting Object Processor). n FOP, développé dans le cadre du projet Apache, permet à partir d’un document XSL-FO de générer un document PDF. Il représente ainsi la solution idéale pour l’édition de factures ou de contrats sur le Web.

Architecture utilisée pour générer du PDF n Les données à présenter sont en XML.

Architecture utilisée pour générer du PDF n Les données à présenter sont en XML. A partir d’une feuille de style XSLT, on génère un document XSLFO, à l’aide d’un processeur XSLT. n On utilise ensuite un processeur FOP pour convertir le document XSL-FO en PDF.

Architecture utilisée pour générer du PDF XML - données XSLT Processeur XSLT Document XSL-FO

Architecture utilisée pour générer du PDF XML - données XSLT Processeur XSLT Document XSL-FO Processeur FOP Document PDF

PRATIQUE XSL-FO

PRATIQUE XSL-FO

Exemple d’organisation d’un fichier XSL-FO n Nous allons utiliser un exemple simple que nous

Exemple d’organisation d’un fichier XSL-FO n Nous allons utiliser un exemple simple que nous avons créé afin de montrer les différentes composantes d’un fichier XSL-FO. n Il est possible de trouver les exemples à l’adresse: http: //tecfa. unige. ch/staf-g/glaus/staf 2 x/ex 3/recette

Fichier XML <? xml version="1. 0" encoding="ISO-8859 -1"? > <? xml-stylesheet href="recettefo. xsl" type="text/xsl"?

Fichier XML <? xml version="1. 0" encoding="ISO-8859 -1"? > <? xml-stylesheet href="recettefo. xsl" type="text/xsl"? > <? cocoon-process type="xslt"? > <!DOCTYPE recette SYSTEM "recette. dtd"> <recette> <titre>Les Bonnes Recettes de Cuisine</titre> <gateau photo="gateau. gif"> <type>Gâteau aux Noix et au Chocolat blanc</type> <ingredients> <ingredient>250 g de chocolat blanc</ingredient> <ingredient>60 g de noix</ingredient> … <ingredient>10 g de levure chimique</ingredient> </ingredients> <preparation> Faites ramollir le beurre dans un petit bol, lui même dans un grand bol d'eau très chaude. fouettez le beurre énergiquement et y ajoutez les sucres. ajoutez l'oeuf entier … </preparation> <internet>http: //www. medi 1. com/scripts/recettes. php 3? r=616</internet> </gateau> </recette>

Feuille de style XSL-FO n On appelle une page XSL-FO de la manière suivante:

Feuille de style XSL-FO n On appelle une page XSL-FO de la manière suivante: <? xml version="1. 0" encoding="ISO-8859 -1"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" xmlns: fo="http: //www. w 3. org/1999/XSL/Format"> <xsl: template match="recette"> <xsl: processing-instruction name="cocoon-format">type="text/xslfo"</xsl: processing-instruction> <fo: root xmlns: fo="http: //www. w 3. org/1999/XSL/Format">

Page layout Page top margin BEFORE P a g e l e ft m

Page layout Page top margin BEFORE P a g e l e ft m a r g i n S T A R T BODY AFTER Page bottom margin P a g e E N D ri g h t m a r g i n

Architecture de la racine FO Sous la racine fo: root il y a toujours:

Architecture de la racine FO Sous la racine fo: root il y a toujours: n un fo: layout-master-set qui définit un ou plusieurs page layout définis avec fo: simple-page-master n des déclarations à option fo: declarations n une séquence d’un ou plusieurs fo: page-sequences qui contiennent du texte (littéralement ou fournis par XSLT) et des instructions de formatage.

Squelette XSL-FO de base <xsl: template match="page"> <fo: root> <fo: layout-master-set> <!-- Definition of

Squelette XSL-FO de base <xsl: template match="page"> <fo: root> <fo: layout-master-set> <!-- Definition of a single master page. It is simple (no headers etc. ) --> <fo: simple-page-master-name="first" > <!-- required element body --> <fo: region-body/> </fo: simple-page-master> </fo: layout-master-set> <!-- Definition of a page sequence --> <fo: page-sequence master-name="first"> <fo: flow-name="xsl-region-body" font-size="14 pt" line-height="14 pt"> <xsl: apply-templates/> </fo: flow> </fo: page-sequence> </fo: root> </xsl: template>

Pagination <fo: layout-master-set> <fo: simple-page-master-name="first" page-height="29. 7 cm" page-width="21 cm" margin-top="1 cm" margin-bottom="2 cm"

Pagination <fo: layout-master-set> <fo: simple-page-master-name="first" page-height="29. 7 cm" page-width="21 cm" margin-top="1 cm" margin-bottom="2 cm" margin-left="2. 5 cm" margin-right="2. 5 cm"> <fo: region-body margin-top="1 cm"/> <fo: region-before extent="1 cm"/> <fo: region-after extent="1. 5 cm"/> </fo: simple-page-master> <fo: simple-page-master-name="right" page-height="29. 7 cm" page-width="21 cm" margin-top="1 cm" margin-bottom="2 cm" margin-left="2. 5 cm" margin-right="2. 5 cm"> <fo: region-body margin-top="1 cm"/> <fo: region-before extent="1 cm"/> <fo: region-after extent="1. 5 cm"/> </fo: simple-page-master>

Pagination <fo: simple-page-master-name="left" page-height="29. 7 cm" page-width="21 cm" margin-top="1 cm" margin-bottom="2 cm" margin-left="2. 5

Pagination <fo: simple-page-master-name="left" page-height="29. 7 cm" page-width="21 cm" margin-top="1 cm" margin-bottom="2 cm" margin-left="2. 5 cm" margin-right="2. 5 cm"> <fo: region-body margin-top="1 cm"/> <fo: region-before extent="1 cm"/> <fo: region-after extent="1. 5 cm"/> </fo: simple-page-master> <fo: page-sequence-master-name="run"> <fo: repeatable-page-master-alternatives maximum-repeats="no-limit" > <fo: conditional-page-master-reference="left" odd-or-even="even" /> <fo: conditional-page-master-reference="right" odd-or-even="odd" /> <fo: conditional-page-master-reference="title"/> </fo: repeatable-page-master-alternatives> </fo: page-sequence-master> </fo: layout-master-set> <!-- end: defines page layout -->

Pagination <!-- actual layout --> <fo: page-sequence master-reference="run" initial-page-number="1"> <fo: static-content flow-name="xsl-region-before"> <fo: block

Pagination <!-- actual layout --> <fo: page-sequence master-reference="run" initial-page-number="1"> <fo: static-content flow-name="xsl-region-before"> <fo: block text-align="end" font-size="10 pt" font-family="sans-serif" line-height="11 pt" color="lightslategray" > <xsl: value-of select="titre"/> <fo: page-number/> </fo: block> </fo: static-content> <fo: flow-name="xsl-region-body" font-size="12 pt" line-height="11 pt"> <xsl: apply-templates/> </fo: flow> </fo: page-sequence> </fo: root> </xsl: template>

Les listes n Mécanisme puissant: listes ordinaires, notes en bas de page, simples tables,

Les listes n Mécanisme puissant: listes ordinaires, notes en bas de page, simples tables, etc. fo: list-block fo: list-item-label fo: list-item-body fo: list-block: contient la liste et contient quelques définitions pour la géométrie n fo: list-item: enfants de fo: list-block, c. a. d. des items qui contiennent un label et un body (voir ci-dessous) n fo: list-item-label: contient le contenu du label (typiquement un fo: block avec un nombre, un caractère dingbat, etc. ) n The fo: list-item-body contient le corps d’un item, un ou plusieurs fo: block n

Une simple « bullet-list » <xsl: template match="ingredients"> <fo: block color="navy" font-size="12 pt" text-align="justify"

Une simple « bullet-list » <xsl: template match="ingredients"> <fo: block color="navy" font-size="12 pt" text-align="justify" space-before. optimum="15 pt" background-color="lavender">Ingrédients : </fo: block> <xsl: apply-templates select="ingredient" /> </xsl: template> <xsl: template match="ingredient"> <fo: list-block space-before. optimum="4 pt"> <fo: list-item-label end-indent="label-end()"><fo: block>·</fo: block> </fo: list-item-label> <fo: list-item-body start-indent="body-start()"> <fo: block text-align="justify" font-size="10 pt"> <xsl: value-of select=". " /> </fo: block> </fo: list-item-body> </fo: list-item> </fo: list-block> </xsl: template> n Résultat : http: //tecfa. unige. ch/staf-g/glaus/staf 2 x/ex 3/recette/liste. pdf

Les tables n ressemblent un peu aux tables HTML fo: table-and-caption fo: table-column n

Les tables n ressemblent un peu aux tables HTML fo: table-and-caption fo: table-column n n n n fo: table-header fo: table-body fo: table-footer fo: table-row fo: table-cell fo: table-and-caption: fo: table-caption: La caption d’une table fo: table: la table proprement dite. Elle contien header et footer à option et un body. fo: table-column: permet de specifier notamment la longeur d’une colonne fo: table-header: Ligne entête, contient des lignes ou cellules fo: table-footer: Ligne "footer", contient des lignes ou cellules fo: table-body: contient des lignes ou cellules fo: table-row: contient des cellules qui contiennent des fo: blocks

Un tableau à deux colonnes <xsl: template match="gateau"> <fo: table-layout="fixed" space-before. optimum="15 pt"> <fo:

Un tableau à deux colonnes <xsl: template match="gateau"> <fo: table-layout="fixed" space-before. optimum="15 pt"> <fo: table-column-number="1" column-width="5 cm" /> <fo: table-column-number="2" column-width="10 cm" /> <fo: table-body> <fo: table-row> <fo: table-cell column-number="1"> <fo: block space-before. optimum="12 pt"> <fo: external-graphic src="{@photo}" /> </fo: block> </fo: table-cell> <fo: table-cell column-number="2"> <fo: block color="navy" font-size="16 pt" text-align="center" spacebefore. optimum="15 pt"> <xsl: apply-templates select="type" /> </fo: block> </fo: table-cell> </fo: table-row> </fo: table-body> </fo: table> </xsl: template> n Résultat : http: //tecfa. unige. ch/staf-g/glaus/staf 2 x/ex 3/recette/table. pdf

Les images n L’insertion d’une image se fait avec le tag : <fo: external-graphic

Les images n L’insertion d’une image se fait avec le tag : <fo: external-graphic src="image"/>

Insertion d’une image <xsl: template match="gateau"> <fo: block color="navy" font-size="16 pt" text-align="center" space before.

Insertion d’une image <xsl: template match="gateau"> <fo: block color="navy" font-size="16 pt" text-align="center" space before. optimum="15 pt"> <xsl: apply-templates select="type" /> </fo: block> <fo: block space-before. optimum="12 pt"> <fo: external-graphic src="{@photo}" /> </fo: block> </xsl: template> Résultat : http: //tecfa. unige. ch/staf-g/glaus/staf 2 x/ex 3/recette/image. pdf

Les liens L’insertion d’un lien Internet ou d’une adresse E-mail se fait avec le

Les liens L’insertion d’un lien Internet ou d’une adresse E-mail se fait avec le tag: <fo: basic-link external-destination="http: //tecfa. unige. ch"> n Autres liens possibles : n – – – – internal-destination indicate-destination show-destination-placement-offset target-presentation-context target-processing-context target-stylesheet

Insertion d’un lien <xsl: template match="internet"> <fo: block color="navy" font-size="12 pt" text-align="justify" space-before. optimum="12

Insertion d’un lien <xsl: template match="internet"> <fo: block color="navy" font-size="12 pt" text-align="justify" space-before. optimum="12 pt" background-color="lavender"> Site Internet : </fo: block> <fo: block color="navy" font-size="12 pt" text-align="justify" space-before. optimum="15 pt"> <fo: basic-link external-destination="{. }"> <xsl: value-of select=". " /> </fo: basic-link> </fo: block> </xsl: template> Résultat : http: //tecfa. unige. ch/staf-g/glaus/staf 2 x/ex 3/recette/lien. pdf

Exemple complet n Résultat final après avoir mis les différents éléments ensemble : http:

Exemple complet n Résultat final après avoir mis les différents éléments ensemble : http: //tecfa. unige. ch/staf-g/glaus/staf 2 x/ex 3/recettefo. xsl n Résultat final en PDF : http: //tecfa. unige. ch/staf-g/glaus/staf 2 x/ex 3/recette. pdf