Page 1 of 1

XML Footnotes tag attributes

PostPosted: Thu May 22, 2014 7:27 pm
by rtmc
I hope I am in the right section.

When I do:

Code: Select all
FootnotesPart fnp = new FootnotesPart(new PartName("/word/footnotes.xml"));
fnp.setJaxbElement ( org.docx4j.jaxb.Context.getWmlObjectFactory().createCTFootnotes() );


and then add to a package, I get this a big <w:footnotes ... > XML

But the footnotes I copied from another document did not work. If I had a footnotespart already created in the document, it works. So, I compared to see the differences.

If I add only this one attribute to the w:footnotes, it works fine.

xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"

I tried this:

Code: Select all
Element n = (Element) fnp.getBinder().getXMLNode(fnp.getJaxbElement());
n.setAttribute("xmlns:wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");


But no luck. What could I do?

Re: XML Footnotes tag attributes

PostPosted: Fri May 23, 2014 12:02 am
by jason
Sorry, your question is not entirely clear.

Are you saying that when you copy a footnote, a namespace is somehow dropped?

Please elaborate by adding XML,and the code you are using to do the copying.

rtmc wrote:n.setAttribute("xmlns:wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");


That won't work; it is up to the XML processor to add namespaces where necessary, and you and docx4j have little control over that.

Re: XML Footnotes tag attributes

PostPosted: Fri May 23, 2014 3:38 am
by rtmc
Ok, I will try a better explanation. You helped me yesterday at stack. I am fixing some issues with my merging code.

The base document, where I concatenate all other, sometimes lack "/word/footnotes.xml". When it is missing, I have to create a new one. So I do

Code: Select all
FootnotesPart fnp = new FootnotesPart(new PartName("/word/footnotes.xml"));
fnp.setJaxbElement( org.docx4j.jaxb.Context.getWmlObjectFactory().createCTFootnotes() );
baseDocument.addTargetPart(fnp);


Then I copy the footnotes from one file to another changing the IDs. The document becomes broken. Except if I already have a "/word/footnotes.xml" in the base document, which works fine. I searched and could not find anything wrong in my method. So I looked at footnotes.xml to compare with the Microsoft one. I realized that the Microsoft have some attributes and tested. I just copied this attribute to the created w:footnotes and zipped again. And it worked.

Created w:footnotes from docx4j:
Code: Select all
<w:footnotes xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ns9="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:ns12="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:ns19="urn:schemas-microsoft-com:office:excel" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:ns21="urn:schemas-microsoft-com:office:powerpoint" xmlns:ns23="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:odx="http://opendope.org/xpaths" xmlns:odc="http://opendope.org/conditions" xmlns:odq="http://opendope.org/questions" xmlns:oda="http://opendope.org/answers" xmlns:odi="http://opendope.org/components" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:ns30="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:ns31="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:ns32="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas">


Microsoft word w:footnotes
Code: Select all
<w:footnotes xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">


All I did MANUALLY was add this attribute to the docx4j w:footnotes:
Code: Select all
xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"


Now, I would like to do it programmatically. Or, if it is a docx4j stuff, you could update.

Thanks

Re: XML Footnotes tag attributes

PostPosted: Fri May 23, 2014 7:20 am
by rtmc
I have tried a LOT of things to edit the xml and add the attribute. No success.

I am almost accepting that the xml can not be edited.

Re: XML Footnotes tag attributes

PostPosted: Fri May 23, 2014 9:01 am
by jason
Which footnote requires that namespace? (Search the XML for "wpc")

What is the XML content of that footnote? Post it here...

Re: XML Footnotes tag attributes

PostPosted: Fri May 23, 2014 9:27 am
by rtmc
Ok, I cant replicate.

Just does not work anymore. I will try to check everything in my code here.

Re: XML Footnotes tag attributes

PostPosted: Fri May 23, 2014 9:34 am
by rtmc
Here is the piece of code if have any clue

first.docx = simple text
second.docx = simple text + footnote reference

Code: Select all
        File first = new File("first.docx");
        File second = new File("second.docx");
       
        WordprocessingMLPackage f = WordprocessingMLPackage.load(first);
        WordprocessingMLPackage s = WordprocessingMLPackage.load(second);
       
        List<CTFtnEdn> listfn;
        try {
            listfn = ((FootnotesPart)f.getParts().get(new PartName("/word/footnotes.xml"))).getJaxbElement().getFootnote();
           
            System.out.println("already had /word/footnotes.xml");
        } catch(Exception ex){
            FootnotesPart fnp = new FootnotesPart(new PartName("/word/footnotes.xml"));
            fnp.setJaxbElement( org.docx4j.jaxb.Context.getWmlObjectFactory().createCTFootnotes() );
            f.addTargetPart(fnp);
            listfn = fnp.getJaxbElement().getFootnote();
           
            System.out.println("added /word/footnotes.xml");
        }
       
        BigInteger ids = new BigInteger("1");
       
        //
        java.util.HashMap<BigInteger, BigInteger> relId = new java.util.HashMap();
        try {
            List<CTFtnEdn> thisFN = ((FootnotesPart)s.getParts().get(new PartName("/word/footnotes.xml"))).getJaxbElement().getFootnote();
           
            for(CTFtnEdn k : thisFN){
                if(k.getId().intValue() != -1 && k.getId().intValue() != 0){
                    relId.put(k.getId(), ids);
                    System.out.println("Copied old id "+k.getId()+" with new id "+ids);
                    k.setId(ids);
                    ids = ids.add(new BigInteger("1"));
                    listfn.add(k);
                }
            }
        } catch(Exception ex){}
       
        List fn = s.getMainDocumentPart().getJAXBNodesViaXPath("//w:footnoteReference", false);
        for(Object el : fn){
            if(el instanceof JAXBElement){
                try {
                    org.docx4j.wml.CTFtnEdnRef o = (org.docx4j.wml.CTFtnEdnRef)((JAXBElement) el).getValue();
                    System.out.println("Replaced old id "+o.getId()+" with new id "+relId.get(o.getId()));
                    o.setId(relId.get(o.getId()));
                } catch(Exception ex){}
            }
        }
        List body = s.getMainDocumentPart().getJAXBNodesViaXPath("//w:body", false);
        for(Object b : body){
            List filhos = ((org.docx4j.wml.Body)b).getContent();
            for(Object k : filhos)
                f.getMainDocumentPart().addObject(k);
        }
       
        File saved = new File("saved.docx");
        f.save(saved);
       
        Desktop.getDesktop().open(saved);


Re: XML Footnotes tag attributes

PostPosted: Fri May 23, 2014 11:55 am
by rtmc
Ok, sorry for wrong question. It was a problem in my code.

I just added this piece of code when creating footnotes part.
Code: Select all
Relationship r = new Relationship();
r.setTarget("footnotes.xml");
r.setType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes");
f.getMainDocumentPart().getRelationshipsPart().addRelationship(r);


Now just the styles missing.