Page 1 of 1

Custom HTML output from comments

PostPosted: Wed Oct 31, 2012 2:18 am
by GeneratedUserName
Hi,

How difficult would it be to take the comment from a .docx file and use that comment as a class or id on the nearest HTML element? Looking through the old source code it looked like it was possible to add a custom XSLT stylesheet (which would make my task very simple, I think anyway), but that method seems to have been deprecated. Thank you for your time...

Re: Custom HTML output from comments

PostPosted: Wed Oct 31, 2012 8:20 am
by jason
You can create your own XSLT from scratch if you wish, but you may find it easier to adapt docx4j's existing HTML output code.

2.8.1 contains 2 ways of generating HTML output.

The first (and primary) way is via XSLT. See HTMLExporterNG2 and docx2xhtmlNG2.xslt.

The other is HtmlExporterNonXSLT, which was added as a proof of concept since running Xalan extension functions on Android is problematic. It is not as complete/mature as the XSLT based approach.

A comment looks something like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:p >
      <w:r>
        <w:t xml:space="preserve">The </w:t>
      </w:r>
      <w:commentRangeStart w:id="0"/>
      <w:r>
        <w:t xml:space="preserve">quick brown </w:t>
      </w:r>
      <w:commentRangeEnd w:id="0"/>
      <w:r>
        <w:rPr>
          <w:rStyle w:val="CommentReference"/>
        </w:rPr>
        <w:commentReference w:id="0"/>
      </w:r>
      <w:r>
        <w:t>fox.</w:t>
      </w:r>
    </w:p>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Since a comment is comprised of "point" tags, not a single XML container, your challenge is to know when you are 'in' a comment. ie you have to maintain state.

Using XSLT, you can maintain state passing a param between templates, or going into a specific mode, or via extension functions. You can do it using any of these approaches (though I'd look at using the modelStates object).

State is easier to maintain using HtmlExporterNonXSLT, since that is pure Java. But putting comments aside, you should try it first to see how far off the HTML output is from your needs.

Hope this helps.. Jason

Re: Custom HTML output from comments

PostPosted: Sat Nov 10, 2012 7:07 am
by GeneratedUserName
I just added a match template for commentRangeStart in docx2xhtmlNG2.xslt .... stuck it back in the jar file and it worked.
I'm not much of a JAVA coder, so this is probably the easiest way. Would it be possible to put the method back on the HtmlExporterNG2 object to supply an external stylesheet? Thanks again for your time!