Page 1 of 1

Issue when trying to bind empty XML node

PostPosted: Mon Dec 02, 2013 7:26 pm
by skiwi
When I try to bind an XML with empty nodes it replaces the content control with "Click here to enter text." where I would expect that the place in the document would just be left blank.

I have modified the sample ContentControlsMergeXML a little to illustrate the issue.

The binding method is called with FLAG_NONE to make sure that the controls are removed in the output document:

Code: Select all
Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_NONE);


In the data XML I have placed a couple of empty nodes (element4, element5 & element6):

Code: Select all
<myxml>
  <element1>hydrogen</element1>
  <element2>helium</element2>
  <element3>lithium</element3>
  <element4/>
  <element5></element5>
  <element6> </element6>
</myxml>


As you can see in the output document element1, element2, element3 and element6 (which has a space as value) are handled correctly, while element 4 and element5 are not.

Any suggestions on how this can be solved?

Thanks!

Re: Issue when trying to bind empty XML node

PostPosted: Wed Dec 04, 2013 8:28 pm
by jason
In your docx4j jar file, you can modify https://github.com/plutext/docx4j/blob/ ... holder.xml

See https://github.com/plutext/docx4j/blob/ ... rXSLT.java st line 686 for how it is used.

Re: Issue when trying to bind empty XML node

PostPosted: Wed Dec 04, 2013 9:13 pm
by skiwi
That did the trick, thanks!

Just a thought: perhaps it's an idea to pull stuff like this out of the jar and into a separate "config" folder or file. That way tampering with the jar wouldn't be necessary.

Re: Issue when trying to bind empty XML node

PostPosted: Thu Dec 05, 2013 8:39 am
by jason

Re: Issue when trying to bind empty XML node

PostPosted: Sat Feb 01, 2014 9:27 am
by jallen
Hi Jason,

This seems like a simple fix, but I am struggling with it. What xml would I put in placeholder.xml in order to not put any placeholder text in. Everything I have tried results in a corrupted word file. The best I can get to work is to use a space. Unfortunately Word still wants to include the space when the document is printed, so that won't work.

Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:rPr>
    <w:rStyle w:val="PlaceholderText"/>
  </w:rPr>
  <w:t xml:space="preserve"> </w:t>
</w:r>


Thanks,
Jeff

Re: Issue when trying to bind empty XML node

PostPosted: Tue Feb 04, 2014 1:39 pm
by jason
Hi Jeff,

Empty w:t should work:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:t/>
</w:r>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


or just empty w:r

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"/>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Please let me know if either of these cause issues for you!

By the way, 3.0.1 will look for a placeholder.xml file on your classpath in dir OpenDoPE, and in the absence of that, fallback to the placeholder.xml file in the docx4j jar.

cheers .. Jason

Re: Issue when trying to bind empty XML node

PostPosted: Wed Feb 05, 2014 3:25 am
by jallen
Thanks for the help as always Jason.

I tried both examples and they both result in a corrupt word document. I included examples for all three scenarios.

This one is the only one I can get to work:
Code: Select all
<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:rPr>
    <w:rStyle w:val="PlaceholderText"/>
  </w:rPr>
  <w:t xml:space="preserve"> </w:t>
</w:r>


Which gives us this in the WordML
Code: Select all
<w:sdtContent><w:r><w:rPr><w:rStyle w:val="PlaceholderText"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r></w:sdtContent>

testgood.docx
(19.55 KiB) Downloaded 294 times


First example gives us this WordML
Code: Select all
<w:sdtContent><w:r><w:t></w:t></w:r></w:sdtContent>

testbad1.docx
(19.52 KiB) Downloaded 310 times


Second example WordML:
Code: Select all
<w:sdtContent><w:r/></w:sdtContent>

testbad2.docx
(19.51 KiB) Downloaded 326 times

Re: Issue when trying to bind empty XML node

PostPosted: Wed Feb 05, 2014 10:17 am
by jason
Per the comment at https://github.com/plutext/docx4j/commi ... 16c3d96f5d
the ability to specify this XML was added as a workaround for issue in some patch levels of Word 2010 x86 where multiline is true

XML from your broken1:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:p >
      <w:sdt>
        <w:sdtPr>
          <w:alias w:val="Enclosure"/>
          <w:tag w:val="#Enclosure"/>
          <w:id w:val="-1403906573"/>
          <w:placeholder>
            <w:docPart w:val="29CFBADFCB2A4E2CA7AE5FD51A4AC762"/>
          </w:placeholder>
          <w:showingPlcHdr/>
          <w:text w:multiLine="true"/>
          <w:dataBinding w:xpath="//document//enclosure" w:storeItemID="{dd92316b-d68b-45b3-b983-0e31a465a2ea}"/>
        </w:sdtPr>
        <w:sdtContent>
          <w:r>
            <w:t></w:t>
          </w:r>
        </w:sdtContent>
      </w:sdt>
    </w:p>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


If I remove <w:text w:multiLine="true"/>, Word 2010 x64 can open it, but displays the placeholder content. But its better to keep w:multiLine="true"

Two possible solutions, both involving hidden text.

1. the first is to go back to using the standard placeholder.xml, but set the style "PlaceholderText" to hidden .. other users have taken this approach

2. the second is to insert a space character, but again, as hidden text.

Hidden text uses w:rPr/w:vanish, so:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
          <w:r>
            <w:rPr>
              <w:vanish/>
            </w:rPr>
            <w:t>   </w:t>
          </w:r>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Note: setting hidden text to show in the Word user interface, but not in
print, is a setting you make to the Word installation

Re: Issue when trying to bind empty XML node

PostPosted: Fri Feb 07, 2014 2:53 am
by jallen
Hi Jason. The hidden text appears to be working good. Thank you for your help!

Jeff