Page 1 of 1

adding xpahts to XPathsPart

PostPosted: Thu Dec 06, 2012 2:41 am
by pamaneg
Hi,

It's possible to programmatically create databound content controls by creating a CTDataBinding (with a custom xpath) within an SdtRun, which is then added to the doc like mainDocumentPart.addObject(sdt);
The databinding in the resulting DOCX works fine when opened in Word.

However, the XPathsPart does not seem to be updated.
How do I add my XPath to the XPathsPart?

thanks,
pamaneg

Re: adding xpahts to XPathsPart

PostPosted: Thu Dec 06, 2012 2:58 am
by pamaneg
just posting some details and clarifications.
the content control is created like this:

Code: Select all
    protected SdtRun createContentControl(String key, String xpath, String storeItemID) {
        SdtRun sdtRun = Context.getWmlObjectFactory().createSdtRun();
        SdtPr sdtPr = Context.getWmlObjectFactory().createSdtPr();
        sdtRun.setSdtPr(sdtPr);
       
        /*
        <w:sdtPr>
          <w:tag w:val="od:xpath=my-xpath-id"/>
          <w:id w:val="-1442757830"/>
          <w:placeholder>
            <w:docPart w:val="DefaultPlaceholder_1082065158"/>
          </w:placeholder>
          <w:dataBinding w:xpath="/MyXml/Duck[1]/@name" w:storeItemID="{6756DA53-8ADB-416F-97AD-557680F6F030}"/>
          <w:text/>
        </w:sdtPr>
        */

        Tag tag = Context.getWmlObjectFactory().createTag();
        tag.setVal("od:xpath=" + key);
        sdtPr.setTag(tag);
        sdtPr.setId();
        CTDataBinding ctDataBinding = Context.getWmlObjectFactory().createCTDataBinding();
        JAXBElement<CTDataBinding> jaxbDB = Context.getWmlObjectFactory().createSdtPrDataBinding(ctDataBinding);
        sdtPr.setDataBinding(ctDataBinding);
        ctDataBinding.setXpath(xpath);
        ctDataBinding.setStoreItemID(storeItemID);

        CTSdtContentRun sdtContent = Context.getWmlObjectFactory().createCTSdtContentRun();
        sdtRun.setSdtContent(sdtContent);
       
        return sdtRun;
    }


which is called like this

Code: Select all
      SdtRun sdt = createContentControl("my-xpath-id", "/MyXml/Duck[1]/@name", storeItemID);
      mainDocumentPart.addObject(sdt);


i did not find any way to add new xPaths to the XPathsPart returned by mainDocumentPart.getXPathsPart()

Re: adding xpahts to XPathsPart

PostPosted: Thu Dec 06, 2012 3:47 am
by pamaneg
I've found a solution:

Code: Select all
   private void addXPathToXPathsPart(String key, String xpath, String storeItemID) {
      ObjectFactory factory = new org.opendope.xpaths.ObjectFactory();

      DataBinding db = factory.createXpathsXpathDataBinding();
      db.setXpath(xpath);
      db.setStoreItemID(storeItemID);

      Xpaths.Xpath xp = factory.createXpathsXpath();
      xp.setDataBinding(db);
      xp.setId(key);
      mainDocumentPart.getXPathsPart().getJaxbElement().getXpath().add(xp);
   }


the actual code was in AbstractMigrator, I just did not look closely enough.