XML To Word (WordML) through XSLT

To translate.

Hey,

Today, a short post on how to use XSLT to create a Word document from a XML file

Process:

1. Create or open the model file with Word, then save it with the xml extension (2003)

2. Open it with a text editor (like notepad++ for instance)

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>

<?mso-application progid=”Word.Document”?>

Suivi d’une balise <w:wordDocument xmlns:aml=”ht….> (ici se trouve toutes les données du document) </w:wordDocument>

3. Modifiez la structure pour avoir au final:

<?xml version=”1.0″ encoding=”Windows-1252″?>

<xsl:stylesheet version=”2.0″

xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”

xmlns:msxsl=”urn:schemas-microsoft-com:xslt”

extension-element-prefixes=”msxsl”

xmlns:aml=”http://schemas.microsoft.com/aml/2001/core”

xmlns:dt=”uuid:C2F41010-65B3-11d1-A29F-00AA00C14882″

xmlns:ve=”http://schemas.openxmlformats.org/markup-compatibility/2006″

xmlns:o=”urn:schemas-microsoft-com:office:office”

xmlns:v=”urn:schemas-microsoft-com:vml”

xmlns:w10=”urn:schemas-microsoft-com:office:word”

xmlns:w=”http://schemas.microsoft.com/office/word/2003/wordml”

xmlns:wx=”http://schemas.microsoft.com/office/word/2003/auxHint”

xmlns:wsp=”http://schemas.microsoft.com/office/word/2003/wordml/sp2″

xmlns:sl=”http://schemas.microsoft.com/schemaLibrary/2003/core”

w:macrosPresent=”no” w:embeddedObjPresent=”no” w:ocxPresent=”no” xml:space=”preserve”>

<xsl:output method=”xml” indent=”no” version=”1.0″

encoding=”Windows-1252″ standalone=”yes”/>

<w:ignoreSubtree w:val=”http://schemas.microsoft.com/office/word/2003/wordml/sp2″/>

<xsl:template match=”/”>

<xsl:processing-instruction name=”mso-application”>

<xsl:text>progid=”Word.Document”</xsl:text>

</xsl:processing-instruction>

<w:wordDocument>

<o:DocumentProperties>

<!– gardez tout ce qui se trouve à l’interieur de Document Properties, de meme pour les blocs suivants: –>

</o:DocumentProperties>

<w:fonts>

</w:fonts>

<w:styles>

</w:styles>

<w:divs>

</w:divs>

<w:shapeDefaults>

</w:shapeDefaults>

<w:docPr>

</w:docPr>

<w:body> <!– ici se trouve le “vrai contenu”

</w:body>

</w:wordDocument>

</xsl:template>

</xsl:stylesheet>

Renommez en .xslt

Voila, vous avez désormais une feuille xslt qui doit vous donner l’exacte réplique de votre page word. Elle n’a rien de dynamique pour le moment mais vous avez la base! Le reste n’est qu’utilisation normale du “langage” xsl!

Pour ajouter un saut de page, la commande est : <w:br w:type=”page”/>

Bonne chance

Leave a Reply

Your email address will not be published. Required fields are marked *