Page 1 of 1

Conditional content binding (ternary operator)

PostPosted: Wed Apr 16, 2025 8:44 am
by nginocchio
Hello,
I was playing around with the OpenDoPE add in in word and I haven't found a way to trigger a ternary content control yet. I apologize if this was covered somewhere, but in my initial research I have yet to find it.
It is possible with two conditionals to render either or but Ideally it would only be one boolean value.

My use case would be

if an xml value within my answer xml evaluates to true I would render some text otherwise another block of text would be rendered in that place.

I have a numbered paragraph where I'd want this to occur
if true:
1. hello the statement evaluated to true
2. other text

if false:
1. this statement is false another block of text
2. other text

I'd appreciate any help.
Thanks

Re: Conditional content binding (ternary operator)

PostPosted: Thu Apr 17, 2025 8:14 pm
by jason
Hi there, you are correct, there isn't a natural way to do this if you want to do it with a single content control

You could potentially do it by putting 2 content controls inside the one expressing the condition. The first one understood to be the "then" result, and the second the "else" result.

Or you could have 2 content controls adjacent to one another, where the first expressed the condition and the "then" result, and the second was the "else" result.

But why require them to be adjacent?

If you allow them to appear in different places in the document, then you are back to what you should be able to do already, which is with a Conditions part contains something like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <condition id="c1">
          <xpathref id="x1"/>
        <condition>
        <condition id="c2">
              <not>
                  <conditionref id="c1"/>
              </not>
        <condition>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Your first content control then has <w:tag w:val="od:condition=c1 "/> and your second <w:tag w:val="od:condition=c2 "/>

It would be up to your editing environment to make this easy for your user to edit, but docx4j should be able to handle it fine. See further org.opendope.conditions.Not and Conditionref