1

When comparing the results of a document transformation process to the original with docx4j, we're getting the following error for one of our test cases:

com.topologi.diffx.xml.UndeclaredNamespaceException: The namespace URI "urn:schemas-microsoft-com:office:office for allowincell" has not been mapped to any prefix.

I eliminated the possibility of errors introduced by the tranformation process by comparing the original document to itself using this method (based on this answer):

private static final String compareToSelf( File fileToCompare ) throws Docx4JException {
    WordprocessingMLPackage olderPackage = WordprocessingMLPackage.load( fileToCompare );
    WordprocessingMLPackage newerPackage = WordprocessingMLPackage.load( fileToCompare );

    Body newerBody = newerPackage.getMainDocumentPart().getJaxbElement().getBody();
    Body olderBody = olderPackage.getMainDocumentPart().getJaxbElement().getBody();

    java.io.StringWriter sw = new java.io.StringWriter();
    javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(sw);
    Calendar changeDate = null;

    Differencer pd = new Differencer();
    pd.setRelsDiffIdentifier("blagh"); // not necessary in this case
    pd.diff( newerBody, olderBody, result, "someone", changeDate,
                newerPackage.getMainDocumentPart().getRelationshipsPart(),
                olderPackage.getMainDocumentPart().getRelationshipsPart() );

    return sw.toString();
}

Stack trace:

com.topologi.diffx.xml.UndeclaredNamespaceException: The namespace URI "urn:schemas-microsoft-com:office:office for allowincell" has not been mapped to any prefix.
    at com.topologi.diffx.xml.NSAwareXMLWriter.getQName(NSAwareXMLWriter.java:604)
    at com.topologi.diffx.xml.NSAwareXMLWriter.attribute(NSAwareXMLWriter.java:527)
    at com.topologi.diffx.event.impl.AttributeEventNSImpl.toXML(AttributeEventNSImpl.java:244)
    at com.topologi.diffx.format.SmartXMLFormatter.format(SmartXMLFormatter.java:212)
    at com.topologi.diffx.sequence.EventSequence.format(EventSequence.java:349)
    at com.topologi.diffx.Docx4jDriver.diff(Docx4jDriver.java:230)
    at org.docx4j.diff.Differencer.diffWorker(Differencer.java:320)
    at org.docx4j.diff.Differencer.diff(Differencer.java:298)
    at exec.DocxCompareTest.compareToSelf(DocxCompareTest.java:212)
    at exec.DocxCompareTest.handleInputFile(DocxCompareTest.java:124)
    at exec.ValidationTest.execute(ValidationTest.java:52)
    at exec.BtbRoundtripTest.main(BtbRoundtripTest.java:13)
java.lang.NullPointerException
    at org.docx4j.diff.Differencer.diffWorker(Differencer.java:377)
    at org.docx4j.diff.Differencer.diff(Differencer.java:298)
    at exec.DocxCompareTest.compareToSelf(DocxCompareTest.java:212)
    at exec.DocxCompareTest.handleInputFile(DocxCompareTest.java:124)
    at exec.ValidationTest.execute(ValidationTest.java:52)
    at exec.BtbRoundtripTest.main(BtbRoundtripTest.java:13)

I've unzipped the *.docx file and the o:allowincell seems to be properly mapped with xmlns:o="urn:schemas-microsoft-com:office:office" in the <w:document> tag of the contained document.xml.

Test document can be dowloaded from here: https://docs.google.com/open?id=0B6_h2TfqvEdeZE43X3RSWnFIZHc

Any ideas on how to resolve this issue?

Community
  • 1
  • 1
Jacob Zwiers
  • 1,092
  • 1
  • 13
  • 33
  • This warning also appears during execution, unsure if it's relevant: `WARN [org.docx4j.openpackaging.contenttype.ContentTypeManager] - ` – Jacob Zwiers Aug 13 '12 at 14:02

1 Answers1

0

In Docx4jDriver at line 192 and 206 there is:

    e.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:w",
            "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

You probably need to add xmlns:o. Ultimately, that code needs to be improved to add all relevant namespaces automatically.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84