Selecting the opposite condition in XPath?

Viewed 96

I have below XML

<Root>
    <Element>
        <batch_id>Jan_22_2021_18</batch_id>
        <proposal_id>130994</proposal_id>
        <proposal_name>Brazil </proposal_name>
        <subregion>1234</subregion>
        <currency>BRL</currency>
    </Element>
    <Element>
        <batch_id>Jan_22_2021_18</batch_id>
        <proposal_id>130994</proposal_id>
        <proposal_name>Brazil </proposal_name>
        <subregion>7225</subregion>
        <currency>BRL</currency>
    </Element>
        <Element>
        <batch_id>Jan_22_2021_18</batch_id>
        <proposal_id>130994</proposal_id>
        <proposal_name>Brazil </proposal_name>
        <subregion>1111</subregion>
        <currency>BRL</currency>
    </Element>
</Root>

below XPaths

  1. Working:

    /Root/Element[(subregion ='7515') or (subregion ='7225') or (subregion ='1234') or (subregion ='8360') or (subregion ='8385') or (subregion ='8435') or (subregion ='8585')]
    
  2. Not working

    /Root/Element[(subregion !='7515') or (subregion !='7225') or (subregion !='1234') or (subregion !='8360') or (subregion !='8385') or (subregion !='8435') or (subregion !='8585')]
    

Can you explain why?

1 Answers

You have a logical flaw.

The opposite of A or B is not not(A) or not(B); it is not(A or B), or equivalently per De Morgan, not(A) and not(B).

So, if you want that other Elements – the ones not selected by your "working" XPath – apply a logical not() to the entire disjunction,

/Root/Element[not((subregion ='7515') 
               or (subregion ='7225') 
               or (subregion ='1234') 
               or (subregion ='8360') 
               or (subregion ='8385') 
               or (subregion ='8435') 
               or (subregion ='8585'))]

or to the individual conditions of the corresponding conjunction:

/Root/Element[    not(subregion ='7515') 
              and not(subregion ='7225') 
              and not(subregion ='1234') 
              and not(subregion ='8360') 
              and not(subregion ='8385') 
              and not(subregion ='8435') 
              and not(subregion ='8585')]
Related