[Tutorial] How to fix SAXParse errors in LibreOffice files

Help with installation and general system troubleshooting questions concerning the office suite LibreOffice.

[Tutorial] How to fix SAXParse errors in LibreOffice files

Postby John_Ha » Fri Jul 28, 2017 5:52 pm

These problems seem only to arise in .docx files saved by LibreOffice.

 Edit: See [Tutorial] Differences between Writer and MS Word files for why you should always work in, and save files, as .odt; and never save a file as a .doc, .docx or .rtf.

Only create a .doc, .docx or .rtf as a copy if you have to, but always keep the .odt as the master.
 

Try one of the following four self-help methods to fix LibreOffice .docx files with SAX parse errors - you only need to use one of them and you chance of a successful recovery is very high.

0. Always work on a copy of the file because, if things go wrong, you will still have the original.

 Edit: When you get the SAXParse error message it tells you the name of the repeated attribute - note it as you will need to search for it.

The error message goes on to say: Do you want to continue to open the file?

If you say YES, everything up to the error is displayed, and everything after the error is missing. If, despite the warning above, you are working on the original file DO NOT NOW SAVE THE FILE (unless you use a different name) BECAUSE YOU WILL DELETE ALL THE MISSING DATA FROM THE .DOCX and you will never then be able to recover the missing data as you will have deleted it from the file. 

1. The free Microsoft Word Viewer seems to be able to open these files ...

... so download it from How to obtain the latest Microsoft Word Viewer. Open the file and copy the data into a Writer document.

Why can Microsoft Word Viewer open them? Presumably because Microsoft Word Viewer has better, more robust error handling code than LO, and can cope with the repeated attribute without throwing an error.

It is also very useful always to have the Viewer available in case you have a document with MS Word Textboxes because AOO does not display Textboxes (though LO does).

2. AOO seems to be able to open these files ...

... so download Apache OpenOffice from http://www.openoffice.org/download/index.html. Create a new user on your PC and install AOO for that user only. AOO and LO seem to interact in that LO grabs some of the AOO properties and this will completely isolate AOO from LO. Open the .docx file with AOO. Save it as a .odt file. Uninstall AOO and delete the added user. Note that this will delete any MS Word Textboxes and their contents because AOO does not support MS Word Textboxes (presumably because they are not part of the OOXML standard).

Why can AOO open them? Presumably because AOO has better, more robust error handling code than LO, and AOO can cope with the repeated attribute without throwing an error.

3. Remove the repeated definitions from document.xml

This requires you to unzip the .docx file, extract the \word\document.xml file, and remove all the occurrences of the repeated attribute specified in the error message you get when you open the .docx file. Note that there may be more than one attribute repeated in the file so you may have to do this for the other repeated attribute(s). Repeated attributes reported here include w:themeShade, w:themeColor and w:cstheme. Some files uploaded to the forum have had many (30+?) repeats.

error message.png
NotePad++ XML Tools plugin error message when opening document.xml says that " w:themeColor " has been re-defined.
This means that there are two (or more) occurrences of " w:themeColor " following each other. There should be only one each time it occurs.
You can also use the XML Tools plugin to check XML syntax which will check to see if you have removed the problem (or have other problems in the file).
error message.png (5.28 KiB) Viewed 5311 times

1 Unzip the .docx file and extract \word\document.xml.

A .docx file is actually a ZIP file so just unZIP it; or rename fred.docx to fred.zip, and double click it.

Clipboard02.png
.docx file when unzipped in 7-ZIP. Double-click \word\ to find document.xml inside

2 Open document.xml in Notepad++ and search for the repeated attribute (eg w:themeColor)

Delete the second instance of w:themeColor="accent1" each time it occurs, leaving the trailing /> as below:

Before correcting ...
Code: Select all   Expand viewCollapse view
<w:rPr>
    <w:sz w:val="20"/>
    <w:szCs w:val="20"/>
    <w:highlight w:val="yellow"/>
    <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
    <w:color w:val="5B9BD5" w:themeColor="accent1" w:themeColor="accent1"/>
</w:rPr>

After correcting ...
Code: Select all   Expand viewCollapse view
<w:rPr>
    <w:sz w:val="20"/>
    <w:szCs w:val="20"/>
    <w:highlight w:val="yellow"/>
    <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
    <w:color w:val="5B9BD5" w:themeColor="accent1"/>
</w:rPr>


theme colour.gif
This image shows document.xml being edited in Notepad++ without invoking the "pretty print" add-on.
Two instances of w:themeColor="accent1" follow each other. Delete the second (and third, fourth etc ...) each time it occurs, always leaving the trailing /

3 Put document.xml back into the .docx file in the \word folder.

If you renamed fred.docx to fred.zip then drag document.xml back into it and rename fred.zip back to fred.docx.

The .docx file should now open properly.

Note that it is easier to find the repeated occurrences if you "pretty print" the XML using the XML Tools plugin for Notepad++. BUT - if you use Pretty print, be sure to Linearise the XML before saving it (it is an XML Tools option) or lots of tabs and newlines will be saved in the file which then appear in the repaired document.

 Edit: Files have recently been analysed where there are additional different errors in the XML. These errors were found and corrected by using the XML Syntax Checker to check the XML. See here 

4. Extract \word\document.xml from the .docx file and strip off all the XML tags to leave just the text

Windows:

Rename the file from fred.docx to fred.ZIP.
Double click fred.ZIP.
Navigate to the \word folder.
Drag document.XML onto the desktop.
- Install Notepad++ and the XML Tools plug-in. Open document.xml with Notepad ++. Go Plugins > XML Tools > Pretty print XML with line breaks. Delete the XML tags leaving just the text.
- Alternatively, Google pretty print and upload document.xml to a pretty print web site which will format it. Delete the XML tags.

Linux:

Rename the file from fred.docx to fred.ZIP.
Unzip fred.ZIP - you may need to install a ZIP utility on Linux.
Navigate to the \word folder.
Extract document.xml.
- Install an XML editor. Open document.xml with the XML editor and format it "pretty print". Delete the XML tags leaving just the text.
- Alternatively, Google pretty print and upload document.xml to a pretty print web site which will format it. Delete the XML tags.

 Edit: The easiest way to delete all the XML tags is with a Find and Replace, where you use a Regular Expression to find all the tags. (Note: A regular expression search will work in LO itself as long as you do not break the character limit for a paragraph [64k in AOO, more in LO??].)

It works fine in NotePad++.

1. Open document.xml.
2. Go Search > Replace ..., with search argument <[^>]+> and replace argument blank (or space).
3. Tick Regular Expressions.
4. Click Replace All.

All XML tags are deleted and you are left with just the text. You will need to re-format it and recreate tables and footnotes etc. If you pretty printed before searching and you do not Linearise the XML before you save the file, you will be left with many tabs which you will need to delete manually. 


If everything else fails, your only hope then is to see [Tutorial] How to find and un-delete Writer temporary files for

a) detailed instructions on how to recover your file as it was when you last opened or saved it, or as it was when it was last saved with AutoRecovery;

b) how to find previous versions of the original file in the folder it is located in, but which have since been deleted;

c) how to un-delete the temporary files Writer wrote while you were editing the file, and then deleted. This will recover your file as it was when you last opened or you last saved it and is probably your best hope.
AOO 4.1.5, Windows 7 Home 64 bit

See the Writer Manual, the Writer FAQ, the Writer Tutorials and the Writer guide.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
 
Posts: 5525
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Return to LibreOffice

Who is online

Users browsing this forum: No registered users and 2 guests