简体   繁体   English

XPath基于多个其他节点的属性排除节点

[英]XPath Exclude nodes based on attributes of multiple other nodes

I'm trying to write XPath against the XML below that will get me all nodes in the list with @id='ResponseActionList' for a specific @rsp_cd where the @act_cd does not exist in the list with @id='ActExcludeList' 我正在尝试针对下面的XML编写XPath,这将使我获得@ id ='ResponseActionList'列表中的所有节点,以获得特定的@rsp_cd,其中@act_cd在具有@ id ='ActExcludeList'的列表中不存在

<resp-env>
   <cmd-resp>
      <static>
         <list id="ResponseActionList">
            <listRow rsp_cd="R1" act_cd="A1" act_tx="A1 Text"/>
            <listRow rsp_cd="R1" act_cd="A2" act_tx="A2 Text"/>
            <listRow rsp_cd="R1" act_cd="A4" act_tx="A4 Text"/>
            <listRow rsp_cd="R1" act_cd="A5" act_tx="A5 Text"/>
            <listRow rsp_cd="R1" act_cd="A6" act_tx="A6 Text"/>
            <listRow rsp_cd="R1" act_cd="A7" act_tx="A7 Text"/>
            <listRow rsp_cd="R2" act_cd="A1" act_tx="A1 Text"/>
            <listRow rsp_cd="R2" act_cd="A2" act_tx="A2 Text"/>
            <listRow rsp_cd="R2" act_cd="A3" act_tx="A3 Text"/>
            <listRow rsp_cd="R2" act_cd="A4" act_tx="A4 Text"/>
            <listRow rsp_cd="R2" act_cd="A5" act_tx="A5 Text"/>
            <listRow rsp_cd="R2" act_cd="A6" act_tx="A6 Text"/>
         </list>
         <list id="ActExcludeList">
            <listRow act_cd="A2"/>
            <listRow act_cd="A3"/>
         </list>
      </static>
   </cmd-resp>
</resp-env>

I can easily get all the nodes containing the appropriate @rsp_cd with this: 我可以通过以下方式轻松获取包含相应@rsp_cd的所有节点:

//static/list[@id='ResponseActionList']/listRow[@rsp_cd='R2']

And I can exclude nodes for a particular @act_cd with this: 我可以使用以下方法排除特定@act_cd的节点:

//static/list[@id='ResponseActionList']/listRow[@rsp_cd='R2' and @act_cd!='A2']

What I want is to exclude all nodes from the first expression above where the @act_cd attribute is not in the results of this expression: 我想从@act_cd属性不在此表达式的结果中的第一个表达式中排除所有节点:

//static/list[@id='ActExcludeList']/listRow/@act_cd

I tried something like this, which I didn't expect to work, and I'm not sure what to try next: 我尝试了这样的事情,但我没想到它会起作用,而且我不确定下一步该怎么做:

//static/list[@id='ResponseActionList']/listRow[@rsp_cd='R2' and not(contains(//static/list[@id='ActExcludeList']/listRow/@act_cd,@act_cd))]

The contains function is used to find substrings in other strings, not values in a node set. contains函数用于查找其他字符串中的子字符串,而不是节点集中的值。 The operator you're looking for is = , which is existentially quantified in XPath - the expression a = b where a and/or b are node sets will succeed if any node in the set a matches any in b . 你要找的运算符是= ,这是在XPath的存在性量化-表达a = b ,其中a和/或b是节点集将成功,如果在设定的任何节点a匹配任何 b

//static/list[@id='ResponseActionList']/listRow[@rsp_cd='R2' and
    not(@act_cd = //static/list[@id='ActExcludeList']/listRow/@act_cd)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM