Page 1 of 1

Conditional binding, XPath problem

PostPosted: Tue May 31, 2011 11:43 pm
by Peter.BY
I have a custom xml where I have a node with boolean value, e.g.

Code: Select all
<root>
  ...
   <required>false</required>
...


I need a condition with xpath that evaluates to true when /root/required text node contains false. I tried various ways, /root/required != true, /root/required = false, not(/root/required). Had no luck. Though, !=, = are valid xpath operators, but it looks like they are not recognized or I use them in a wrong way.
Besides, is there a way to use XPath functions (those that under http://www.w3.org/2005/xpath-functions namespace)?
I really appreciate any help.

Re: Conditional binding, XPath problem

PostPosted: Wed Jun 01, 2011 1:45 am
by jason
Hi

The idea is that you should be able to use everything which the XPath implementation used in docx4j's XmlUtils supports in your XPath expressions. This should include the functions in the 1999 XPath v1 spec; I'd need to do some research re the XQuery 1.0 and XPath 2.0 functions ... this is a relevant consideration in choice of XPath engine.

Try xpath="string(/yourxml)='false'" or in your example xpath="string(/root/required)='false'"

This works for me (using current svn).

Here is how the various bits look:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting

<pkg:part pkg:name="/customXml/item2.xml" pkg:contentType="application/xml" pkg:padding="32">
  <pkg:xmlData>
    <conditions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://opendope.org/conditions">
      <condition id="c1">
        <xpathref id="x1"/>
      </condition>
      <condition id="c1not">
        <xpathref id="x1not"/>
      </condition>
    </conditions>
  </pkg:xmlData>
</pkg:part>

<pkg:part pkg:name="/customXml/item5.xml" pkg:contentType="application/xml" pkg:padding="32">
  <pkg:xmlData>
    <xpaths xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://opendope.org/xpaths">
      <xpath id="x1" questionID="q1">
        <dataBinding xpath="/yourxml" storeItemID="{C8DB1CB5-0B4A-4448-A27F-F9C262073893}"/>
      </xpath>
      <xpath id="x1not" questionID="q1">
        <dataBinding xpath="string(/yourxml)='false'" storeItemID="{C8DB1CB5-0B4A-4448-A27F-F9C262073893}"/>
      </xpath>
    </xpaths>
  </pkg:xmlData>
</pkg:part>

<pkg:part pkg:name="/customXml/item3.xml" pkg:contentType="application/xml" pkg:padding="32">
  <pkg:xmlData>
    <yourxml>true</yourxml>
  </pkg:xmlData>
</pkg:part>

<pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">    
    <pkg:xmlData>
      <w:document >
        <w:body>
          <w:sdt>
            <w:sdtPr>
              <w:tag w:val="od:condition=c1"/>
            </w:sdtPr>
            <w:sdtContent>
              <w:p>
                <w:r>
                  <w:t>FLIP: If this paragraph is in....</w:t>
                </w:r>
              </w:p>
            </w:sdtContent>
          </w:sdt>
          <w:sdt>
            <w:sdtPr>
              <w:tag w:val="od:condition=c1not"/>
            </w:sdtPr>
            <w:sdtContent>
              <w:p>
                <w:r>
                  <w:t>.. then FLOP this should be out.</w:t>
                </w:r>
              </w:p>
            </w:sdtContent>
          </w:sdt>
        </w:body>
      </w:document>
    </pkg:xmlData>
  </pkg:part>

  <pkg:part pkg:name="/customXml/item4.xml" pkg:contentType="application/xml" pkg:padding="32">
    <pkg:xmlData>
      <questionnaire xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://opendope.org/questions">
        <questions>
          <question id="q1">
            <text>Flip or flop?</text>
            <response>
              <fixed canSelectMany="false">
                <category>
                  <label>flip</label>
                  <value>true</value>
                </category>
                <category>
                  <label>flop</label>
                  <value>false</value>
                </category>
              </fixed>
            </response>
          </question>
        </questions>
      </questionnaire>
    </pkg:xmlData>
  </pkg:part>


 
Parsed in 0.007 seconds, using GeSHi 1.0.8.4


I'll look further into what formulations should and shouldn't work, and improve the documentation. Thanks for your feedback.

cheers .. Jason

ps the conditions.xsd and generated classes support using boolean combinations of conditions, so in principal, you could do your "not" in the condition, rather than the XPath. However, support for this is not yet implemented in docx4j's OpenDoPEHandler

Re: Conditional binding, XPath problem

PostPosted: Wed Jun 01, 2011 3:20 am
by Peter.BY
With your advice on xpath I was able to check if the text node is empty, that was my next need :). You saved me a lot of time, thanks!