xml - 在 XPath 中选择相反的条件?

标签 xml xpath boolean-logic

我有以下 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>

在 XPath 之下

  1. 工作:

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

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

你能解释一下为什么吗?

最佳答案

你有一个逻辑错误。

A 或 B 的反义词是not not(A) or not(B);它是 not(A or B),或者相当于 De Morgan , not(A) and not(B).

因此,如果您想要其他 Elements – 那些未被您的“工作”XPath 选择的元素 – 对整个析取应用逻辑 not()

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

或者对应连词的个别条件:

/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')]

关于xml - 在 XPath 中选择相反的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65908736/

相关文章:

java - 在 xml View 中使用 Selenium 1.82 By.xpath 会出现 NoSuchElementException

python - 如何使用 scrapy 抓取应用程序的 google play 评论?

C++ 服务器程序 Print While Loop

javascript - 无法从 xml 文件获取数据

java - 使用字符串变量从 xml 返回 getString?

html - XPath:我可以选择下一个(或上一个)兄弟符合某些条件的节点吗?

c - 在 C 的条件之外的语句中使用 '&&' 和 '||'

boolean - 异或多数代数逻辑

xml.Unmarshall 多根 XML 在 Go

.net - 给定 XElement,如何在给定 XPath 的情况下检索对另一个相关 XElement/Xattribute 的引用?