Documentation/FR/Calc/Import ou export via filtre XML ou XSLT
Cette page est en PROJET et a besoin de votre participation pour sa rédaction.
Contents
Introduction
Depuis la version 2.x d'OpenOffice, les documents sont enregistrés au format OASIS OpenDocument sous la forme de fichiers XML compressés. En outre OpenOffice supporte également un grand nombre d'autres formats de fichier plus courants comme ceux de la suite Microsoft. Et si tous les formats de fichiers disponibles ne suffisent pas, il y a aussi la possibilité d'ajouter de nouveaux filtres d'importation et d'exportation.
Dans cet exemple, on vous montre comment créer vos propres filtres d'importation et d'exportation sous Calc.
Ressources additionnelles
Pour obtenir la liste complète des noms des balises utilisées dans les documents OpenOffice, se référer à la spécification du format XML à la page suivante : http://xml.openoffice.org/. Template:Documentation/Note
Les fichiers
Fichier source de l'exemple
Pour notre exemple, nous allons utiliser un fichier XML simple avec 4 types de données : - purpose, du texte simple, - amount, un nombre avec 2 chiffres après la virgule, - tax, un nombre avec 4 chiffres après la virgule ( bien que des taxes avec autant de précision soient un peu irréalistes ), - maturity, une date.
Le fichier XML de l'exemple est constitué de plusieurs paiements contenant ces champs. Voici le contenu du fichier :
<?xml version="1.0"?> <payments>
<payment> <purpose>CD</purpose> <amount>12.95</amount> <tax>19.1234</tax> <maturity>2008-03-01</maturity> </payment> <payment> <purpose>DVD</purpose> <amount>19.95</amount> <tax>19.4321</tax> <maturity>2008-03-02</maturity> </payment> <payment> <purpose>Clothes</purpose> <amount>99.95</amount> <tax>18.5678</tax> <maturity>2008-03-03</maturity> </payment> <payment> <purpose>Book</purpose> <amount>9.49</amount> <tax>18.9876</tax> <maturity>2008-03-04</maturity> </payment>
</payments>
Enregistrez le fichier en tant que payments.xml, par exemple dans le dossier C:\Temp
Les filtres
Filtre d'import XSLT
Pour importer le fichier XML d'exemple dans OOo Calc, nous avons besoin d'une feuille de style XSLT. Cette feuille de style créé un fichier XML utilisable par Calc. En voici le code source :
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0">
<office:automatic-styles>
<style:style style:name="co1" style:family="table-column"> <style:table-column-properties fo:break-before="auto" style:column-width="6.000cm"/> </style:style> <style:style style:name="co2" style:family="table-column"> <style:table-column-properties fo:break-before="auto" style:column-width="3.000cm"/> </style:style>
<number:date-style style:name="N36" number:automatic-order="true"> <number:day number:style="long"/> <number:text>.</number:text> <number:month number:style="long"/> <number:text>.</number:text> <number:year number:style="long"/> </number:date-style> <number:number-style style:name="N107"> <number:number number:decimal-places="4" number:min-integer-digits="1"/> </number:number-style>
<style:style style:name="ce1" style:family="table-cell" style:parent-style-name="Default"> <style:table-cell-properties style:text-align-source="fix" style:repeat-content="false"/> <style:paragraph-properties fo:text-align="end"/> </style:style> <style:style style:name="ce2" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N107"/> <style:style style:name="ce3" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N36"/>
</office:automatic-styles>
<office:body> <office:spreadsheet> <table:table>
<table:table-column table:style-name="co1" table:default-cell-style-name="Default"/> <table:table-column table:style-name="co2" table:default-cell-style-name="Default"/> <table:table-column table:style-name="co2" table:default-cell-style-name="ce2"/> <table:table-column table:style-name="co2" table:default-cell-style-name="ce3"/>
<table:table-row> <table:table-cell><text:p>Purpose</text:p></table:table-cell> <table:table-cell table:style-name="ce1" office:value-type="string"><text:p>Amount</text:p></table:table-cell> <table:table-cell table:style-name="ce1" office:value-type="string"><text:p>Tax</text:p></table:table-cell> <table:table-cell table:style-name="ce1" office:value-type="string"><text:p>Maturity</text:p></table:table-cell> </table:table-row>
<xsl:for-each select="payments/payment"> <table:table-row>
<table:table-cell> <text:p><xsl:value-of select="purpose"/></text:p> </table:table-cell>
<table:table-cell office:value-type="float"> <xsl:attribute name="office:value"><xsl:value-of select="amount"/></xsl:attribute> <text:p><xsl:value-of select="amount"/></text:p> </table:table-cell>
<table:table-cell office:value-type="float"> <xsl:attribute name="office:value"><xsl:value-of select="tax"/></xsl:attribute> <text:p><xsl:value-of select="tax"/></text:p> </table:table-cell>
<table:table-cell office:value-type="date"> <xsl:attribute name="office:date-value"><xsl:value-of select="maturity"/></xsl:attribute> <text:p><xsl:value-of select="maturity"/></text:p> </table:table-cell>
</table:table-row> </xsl:for-each> </table:table> </office:spreadsheet> </office:body> </office:document-content>
</xsl:template> </xsl:stylesheet>
Côté programmation Basic
(en attente)