Page 1 of 1

Convert XML to Office Document

PostPosted: Thu Jan 03, 2013 11:36 am
by nsamy
Hi Team,

I am new to open office and I have installed Open Office, Now I need to convert an XML to office document with OpenOffice API. Please guide me.\

Thanks.

Re: Convert XML to Office Document

PostPosted: Thu Jan 03, 2013 4:56 pm
by Villeroy
Write an XSLT script to convert your XML to the correct ODF file type and install your script under Tools>"XML Filters". Then you simply open your files and save it as ODF file.

Re: Convert XML to Office Document

PostPosted: Fri Jan 04, 2013 1:35 pm
by nsamy
Hi Villeroy, Thanks for your reply. Please elaborate the above process.
Since I am new to XSLT, Please let me know which tool should I use to write XSLT script ?
Should I save XML file as ODF file ?

Re: Convert XML to Office Document

PostPosted: Fri Jan 04, 2013 3:01 pm
by Villeroy

Re: Convert XML to Office Document

PostPosted: Sat Jan 05, 2013 9:56 pm
by rudolfo
If you want to work with XSLT make sure you have the XSLT filters installed. You need to explicitly select them in customized installation mode, they won't get installed by default.
The XSLT processing in OpenOffice is based on Java tools, so it requires a correct setup of the Java Runtime (On MS Windows Java 6 and 32bit is working reliably with OOo, a 64bit Java cannot work with OOo).
If you have never seen an XSLT file before, I recommend to have a look at the docbook export/import Filter. It can be found inthe $OOoINSTALLDIR/Basis/share/xslt/docbook directory as the file sofftodocbookheadings.xsl (read it as StarOffice2docbook). Look at it together with a good Tutorial about XSLT.
You may probably follow the second .xsl file in that directory for importing/converting generic XML to ODF, but the export filter sofftodocbookheadings.xsl has more explaining comments.
If you are looking for a tool Apache Ant or if you want to go with a full blown IDE Eclipse might be helpful.

Re: Convert XML to Office Document

PostPosted: Sun Jan 20, 2013 10:25 am
by jza
I remember there was an XSLT template, so that users dont need to go through the whole spec of the Spreadsheet, this was used to convert to HTML, DocBooks and others. I cant remember where exactly it was.
There are some basic, python and java samples as well on using the XML parsing features.
http://incubator.apache.org/odftoolkit/ ... unner.html and http://books.evc-cit.info/odbook/apb.html

Code: Select all   Expand viewCollapse view
<xsl:template match="document">
    <html>
        <head>
            <title>My Document</title>
        </head>
        <body>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>

<xsl:template match="para">
    <p>
        <xsl:apply-templates/>
    </p>
</xsl:template>

<xsl:template match="index">
    <h1>Index</h1>
    <ul>
    <xsl:apply-templates/>
    </ul>
</xsl:template>

<xsl:template match="item">
    <li>
        <xsl:apply-templates/>
    </li>
</xsl:template>

<xsl:template match="endnote">
    <h3><xsl:apply-templates/></h3>
</xsl:template>


here is some python script interacitng with ODS:
http://books.evc-cit.info/odbook/ch05.h ... et-section
Code: Select all   Expand viewCollapse view
import xml.dom
import xml.dom.ext
import xml.dom.minidom
import xml.parsers.expat
import sys
import od_number
from zipfile import *
from StringIO import *

if (len(sys.argv) == 4):
   
    #   Open an existing OpenDocument file
    #
    inFile = ZipFile( sys.argv[1] )
   
    #   ...and a brand new output file
    #
    outFile = ZipFile( sys.argv[2], "w", ZIP_DEFLATED );
   
    getParameters( sys.argv[3] )
   
    #
    #   modify all appropriate currency styles
    #
    fixCurrency( "styles.xml" )
    fixCurrency( "content.xml" )

    #
    #   copy the manifest
    #
    copyManifest( )
   
    inFile.close
    outFile.close
else:
    print "Usage: " + sys.argv[0] + " inputfile outputfile parameterfile"


There is also the flat XML format although not sure if it can be used for spreadsheets.