Page 1 of 1

unexpected element br

PostPosted: Fri Mar 21, 2014 3:10 am
by nrg
Hello,

i use the docx4j-api to merge docx-files. the docx-files are dynamic generated by an engine and i suspect the engine is using elements which are not defined in the xsd. (edit: or rather not in the right context)

document.xml snippet:
Code: Select all
<w:t>Salutation,<w:br/><w:br/>Text</w:t>


warning:
Code: Select all
WARN  JaxbValidationEventHandler - [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"br"). Expected elements are <{ }text>
INFO  JaxbValidationEventHandler - continuing (with possible element/attribute loss)


the result is element loss :(. is there any way to handle this?

thanks
nrg

Re: unexpected element br

PostPosted: Fri Mar 21, 2014 7:18 am
by jason
Your diagnosis is correct; w:br should be a sibling of w:t (with parent w:r); w:t contains only text.

Out of interest, what engine is producing this?

You can workaround this issue by modifying:

https://github.com/plutext/docx4j/blob/ ... essor.xslt

(The rule at line 93 is a workaround for a similar issue in Google Docs)

Re: unexpected element br

PostPosted: Fri Mar 21, 2014 8:48 pm
by nrg
hello jason,

thanks for your response. the sources let me know the xsl is packed into the jar-file. http://www.docx4java.org/svn/docx4j/trunk/docx4j/src/main/java/org/docx4j/jaxb/JaxbValidationEventHandler.java

so i have to edit the jar-file, havent i? is there any way to make this workaround "update-safe"?

and can you help me with the xsl? i didnt get it :(.

the engine is a solution for ecm - not really popular.

thanks
nrg

Re: unexpected element br

PostPosted: Fri Mar 21, 2014 9:54 pm
by jason
It may be time to provide for user override of that XSLT file.

How about:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <xsl:template match="w:t">
    <xsl:for-each select="node()">

      <xsl:choose>
        <xsl:when test="self::text()">
          <w:t>
            <xsl:copy-of select="."/>
          </w:t>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="."/>
        </xsl:otherwise>        
      </xsl:choose>

    </xsl:for-each>
   
  </xsl:template>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

Re: unexpected element br

PostPosted: Fri Mar 21, 2014 10:24 pm
by nrg
throws an expcetion:

org.apache.xpath.domapi.XPathStylesheetDOM3Exception: the prefix must be resolved to a namespace: w

at line:
<xsl:template match="w:t">

tried it with @w:t, same result

sorry, havent done much with xslt so far. thanks for your help

Re: unexpected element br

PostPosted: Fri Mar 21, 2014 11:21 pm
by nrg
i've created an example to reproduce the issue.

Code: Select all
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class Test {

   public static void main(String[] args) throws FileNotFoundException, IOException, Docx4JException {
      File in = new File("C:/test.docx");
      File out = new File("C:/test_out.docx");
      
      WordprocessingMLPackage docx = WordprocessingMLPackage.load(in);
      docx.getMainDocumentPart().addObject(Context.getWmlObjectFactory().createBr());
      docx.save(out);
   }

}

Re: unexpected element br

PostPosted: Sat Mar 22, 2014 7:11 am
by jason
You have to add the template to the xslt mc-preprocessor.xslt

Or to some other xslt which declares xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"

Re: unexpected element br

PostPosted: Mon Mar 24, 2014 7:37 pm
by nrg
that's what i did. but i have only modified the xsl at line 89 and forget to add the namespace at line 7 :oops:.

now it works! thanks