0

I've been able to set, via code, the xpaths for the placeholders found in the document.


for (Object o : finderSdtRun.results) {

            if (o instanceof SdtRun){

                SdtPr sdtPr=((SdtRun) o).getSdtPr();
                Tag t = sdtPr.getTag();                 
                CTDataBinding ctDataBinding = Context.getWmlObjectFactory().createCTDataBinding();
                //JAXBElement jaxbDB = Context.getWmlObjectFactory().createSdtPrDataBinding(ctDataBinding);
                sdtPr.setDataBinding(ctDataBinding);
                ctDataBinding.setXpath("tuttappostaferragost");
                ctDataBinding.setStoreItemID("something");          

                ObjectFactory factory = new org.opendope.xpaths.ObjectFactory();




                DataBinding db = factory.createXpathsXpathDataBinding();
                db.setXpath("tuttappostaferragost");
                db.setStoreItemID("something");

                Xpaths.Xpath xp = factory.createXpathsXpath();
                xp.setDataBinding(db);
                xp.setId("something");



          try {
                    wordMLPackage.getMainDocumentPart().getXPathsPart().getContents().getXpath().add(xp);
                } catch (Docx4JException e) {
                    e.printStackTrace();
                }       

                ;

The problem is that, once set, they are not recognized by word, so I thought to add the created Xpaths to a new XpathPart, and then add it to the main Document part. But I failed because the method:

 wordMLPackage.getMainDocumentPart().getXPathsPart() 

returns null. This sounded reasonable, since only content control was set, without any Xpath.

Then I set the Xpaths via content control toolkit and the same line of code like above, returned me null, which added a lot of confusion in my yet confused ideas.

Is there any way to tell the document that new Xpath have been added to the document? I mean, if there is a way to add Xpath via code (the w:databinding w:storedItemId tags), why it is not possible to make it work?

In general I want to add Xpath and all information necessary, via code, avoiding the use of any toolkit. Thank you :D

  • Sounds like the problem is with your XPath, not with the code as it. Please share the constructed XPath string (after construction) and a partial, small, but valid, input XML document that we can test the XPath against. If you haven't done so yourself yet, then that's the first place to try to get this to work. – Abel Sep 15 '15 at 10:05

1 Answers1

1

First, you have to decide whether you want plain old Word databinding, or the additional OpenDoPE capabilities (which use the content control tag to support repeats, conditionals etc).

You only need an XPaths part if you are using the OpenDoPE extensions.

I'll assume for now that you are just looking to do basic Word content control databinding.

To set that up programmatically, you need to add a custom xml part, and a rel from it to its itemProps.xml part, which contains something like:

<ds:datastoreItem ds:itemID="{5448916C-134B-45E6-B8FE-88CC1FFC17C3}" xmlns:ds="http://schemas.openxmlformats.org/officeDocument/2006/customXml">
  <ds:schemaRefs/>
</ds:datastoreItem>

(to add a part B to part A, use partA.addTargetPart)

You can see it is this part with gives the custom xml part its itemID; this corresponds with the value you set in:

        DataBinding db = factory.createXpathsXpathDataBinding();
        db.setStoreItemID("something");

Then, set the XPath via the method you were using.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • Thank you @JasonPlutext. I've been able to create the CustomXMLDataStoragePart with all references, and then adding the Xpaths, everything is ok. I've created a temp CustomXMLDataStoragePart and then I injected my XML for binding without passing through Docx4J.bind(), but using the method customXmlDataStoragePart.getData().setDocument(xmlStream). Now, I'd like to remove content control , for pdf conversion, so I tried the Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_BIND_REMOVE_SDT) method, but it doesn't work. Any advice? – Danilo Petrone Sep 16 '15 at 12:55
  • RemovalHandler only strips OpenDoPE content controls; it leaves the others: https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/model/datastorage/RemovalHandler.xslt It would be simple to extend it. – JasonPlutext Sep 16 '15 at 22:09
  • It's ok, I will try it (even if I don't know where to start :D). But, just for completeness: when I used for the first time Docx4J.bind(..., FLAG_INSERT_XML & FLAG_BIND_REMOVE_SDT), on a docx which was full of standard content control (no OpenDoPe), after injecting my XML, so my data, everything was fine, I mean, all sdt's were removed without affecting the content, so why? (if could give me some piece of advice on how to integrate these changes to make it work, I'd really appreciate it). Thank you! :D – Danilo Petrone Sep 17 '15 at 07:52