Search code examples
xpath

Need Exclusion XPath Example


For the xml below I only want to return the value of "DD" if "Q1" does NOT have a value of "Q1Yes".

<form>
<field name="ImageQualityRepeating" visible="true">
<rowinstance entryMod="Daily Diary" id="Daily Diary_1689194865577:0" index="1" lockedRow="false" newrow="true">
    <field name="Q1" visible="true">Q1Yes</field>
    <field name="Q1Yes" visible="true">true</field>
    <field name="QCDone" visible="true">QCDoneCheck</field>
    <field name="DD" visible="true">1</field>
</rowinstance>
<rowinstance entryMod="Daily Diary" id="Daily Diary_1689194865677:0" index="1" lockedRow="false" newrow="true">
    <field name="Q1" visible="true"/>
    <field name="Q1Yes" visible="true">true</field>
    <field name="QCDone" visible="true">QCDoneCheck</field>
    <field name="DD" visible="true">2</field>
</rowinstance>
</field>
</form>

For returning each now I have

/form/field[@name='ImageQualityRepeating']/rowinstance/field[@name='Q1' and @visible='true']/text()

returns "Q1Yes"

/form/field[@name='ImageQualityRepeating']/rowinstance/field[@name='DD' and @visible='true']/text()

returns "1"


Solution

  • There are a lot of Xpath to succes, but in this case I prefer to put a predicate on rowinstance like this rowinstance[not(field[@name='Q1']='Q1Yes')]

    and than get the correct value of field with the @name='DD'.

    The XPath will be:

    /form/field/rowinstance[not(field[@name='Q1']='Q1Yes')]/field[@name='DD']/text()