Page 1 of 1

Content control repeat - detect last element

PostPosted: Thu Jul 27, 2017 10:27 pm
by Elimas
I'm using content controls binding in docx4j to generate word document. I have repeat content control which prints elements in word as text. Each element is separated by ", \r\n".
I'd like to ask if it's possible to make condition with XPath which will check if current iteration is last one, or to check current iteration index. That would allow me to skip seperator for last element.
Example:
XML:
Code: Select all
<elements>
<element>el1</element>
<element>el2</element>
<element>el3</element>
</elements>

Current output:
Code: Select all
el1,
el2,
el3,


I'd like to make output:
Code: Select all
el1,
el2,
el3

I checked XPath position() but it printed -1.

Re: Content control repeat - detect last element

PostPosted: Fri Jul 28, 2017 7:45 pm
by jason
There is od:RptPosCon; I will write something explaining how to use it in a day or so.

Re: Content control repeat - detect last element

PostPosted: Tue Aug 01, 2017 11:49 am
by jason
Please find sample attached.

Removing some stuff for clarity, it contains:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:sdt>
      <w:sdtPr>
        <w:alias w:val="Repeat: wUz7G"/>
        <w:tag w:val="od:repeat=wUz7G"/>
      </w:sdtPr>
      <w:sdtContent>
        <w:p >
          <w:sdt>
            <w:sdtPr>
              <w:alias w:val="Data value: CpZN3"/>
              <w:tag w:val="od:xpath=CpZN3"/>
              <w:dataBinding w:xpath="/elements[1]/element[1]" w:storeItemID="{1AB8AB41-29D1-436E-ACF3-78F891FDF452}"/>
              <w:text w:multiLine="1"/>
            </w:sdtPr>
            <w:sdtContent>
              <w:r>
                <w:t>el1</w:t>
              </w:r>
            </w:sdtContent>
          </w:sdt>
          <w:sdt>
            <w:sdtPr>
              <w:tag w:val="od:RptPosCon=GekJx"/>
            </w:sdtPr>
            <w:sdtContent>
              <w:r>
                <w:t>,</w:t>
              </w:r>
            </w:sdtContent>
          </w:sdt>
        </w:p>
      </w:sdtContent>
    </w:sdt>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


in other words, a repeat with a bind in it, followed by an sdt with od:RptPosCon tag.

That tag is like a condition, in that its content only appears if it is "true". But unlike a condition, the tag points directly to an entry in the XPaths part (ie NOT the conditions part). The easiest way to add od:RptPosCon in the authoring tool is to add/specify an XPath, then alter the tag from od:xpath to od:RptPosCon (which you can do via the Word Developer tab).

You should be able to use an XPath such as:

Code: Select all
// If first entry in repeat
position()=1

// If not the first entry in repeat
position()&gt;1"

// If second entry in repeat
position()=2

// if second last entry in repeat
position()=last()-1"

// if last entry in repeat
position()=last()

// not last
position()!=last()


In dusting off this code, I had to make a minor fix:- https://github.com/plutext/docx4j/commi ... fc7f75ec87

It won't work without that...

Re: Content control repeat - detect last element

PostPosted: Thu Aug 03, 2017 9:41 pm
by jason