Page 1 of 1

Use repeat in answers opendope binding

PostPosted: Wed Dec 07, 2022 6:38 pm
by alexandre
Hello,

I've followed this user guide to implement condition and repeat : https://www.docx4java.org/blog/2018/04/ ... a-binding/.

I've installed the no-xml authoring and set up some conditions and repeat tags in my docx document. In my code I've written :

Answers answers = new Answers();
addAnswer(answers,"key_Sn", "valuexx"); //the condition tag
addAnswer(answers,"PleaseRepeat_xF", "2"); //the repeat tag
Docx4J.bind(wordMLPackage, answers, Docx4J.FLAG_BIND_INSERT_XML + Docx4J.FLAG_BIND_BIND_XML);

So the conditional part is working but I've never managed to make the repeat part's working. The selected text seems disappearing. Repeating 0 times. Is it the right way to implement it ? Adding a "number" seems the natural thing to do.

Thank you,
Alexandre

Re: Use repeat in answers opendope binding

PostPosted: Tue Dec 20, 2022 9:14 am
by jason
Hi Alexandre

Sorry for the delay in replying, been a bit busy here.

You can populate an Answers object programmatically, as shown in the blog post.

Or you can process a pre-existing Answers file (ie one created as output from some other system, either interactively or not). This is the way it would usually be done, and the sample code for processing this can be found at https://github.com/plutext/docx4j/blob/ ... geXML.java

It is in constructing the Answers object that you'd use the "number".

By the time you have an Answers object, the number of rows in the repeat is given by counting them. A minimal example (with a row count of 2):

Code: Select all
<oda:answers xmlns:oda="http://opendope.org/answers">
    <oda:repeat qref="PleaseRepeat_Yg">
        <oda:row/>   <------- typically there'd be answers nested in here
        <oda:row/>
    </oda:repeat>
</oda:answers>


The code for populating this Answers object programmatically and binding it:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                Answers answers = new Answers();
                Repeat r = new Repeat();
                r.setQref("PleaseRepeat_Yg");
                // 2 rows
                r.getRow().add(new Repeat.Row());  // usually there'd be more answers inside a Row
                r.getRow().add(new Repeat.Row());
               
                answers.getAnswerOrRepeat().add(r);
               
                System.out.println(XmlUtils.marshaltoString(answers));
                Docx4J.bind(wordMLPackage, answers, Docx4J.FLAG_BIND_INSERT_XML | Docx4J.FLAG_BIND_BIND_XML);

 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


In your case it would be r.setQref("PleaseRepeat_xF")