2

我正在使用 VB.net (2003),并在 xml 文档上调用 SelectNodes 方法。
如果我有文件:

<InqRs>  
  <DetRs>  
    <RefInfo>  
      <RefType>StopNum</RefType>  
      <RefId>0</RefId>  
    </RefInfo>  
    <RefInfo>  
      <RefType>Id</RefType>  
      <RefId>0</RefId>  
    </RefInfo>  
  </DetRs>  
  <DetRs>  
    <RefInfo>  
      <RefType>StopNum</RefType>  
      <RefId>0</RefId>  
    </RefInfo>  
    <RefInfo>  
      <RefType>Id</RefType>  
      <RefId>1</RefId>  
    </RefInfo>  
  </DetRs>  
</InqRs>

如何仅选择DetRs具有RefType=Idand的那个RefId=0,即上面的“第一个”?

我尝试了几种不同的尝试,其中包括:

InqRs/DetRs[RefInfo/RefType='Id' and RefInfo/RefId='0']  
InqRs/DetRs[RefInfo/RefType='Id'][RefInfo/RefId='0']  

但是这些选择了两个 DetRs 部分(因为我认为 StopNum RefId 为 0)。

4

2 回答 2

3

您想要DetRs顶部元素的所有子元素:

    /*/DetRs

那有一个RefInfo孩子:

    /*/DetRs
             [RefInfo]

具有RefType价值“ Id”:

    /*/DetRs
             [RefInfo
                   [ RefType=' Id']
              ]

并且RefId具有值为0

    /*/DetRs
             [RefInfo
                   [ RefType=' Id'
                 and
                   RefId=0
                   ]
              ]

而且这个 XPath 表达式正确地选择DetRs了提供的 XML 文档中想要的第一个元素

当然,如果某人有其他风格偏好,上述表达式也可以写成:

    /*/DetRs[RefInfo[RefType='Id' and RefId=0]]

于 2008-12-14T18:44:40.900 回答
1

像这样。您不需要 XPath 表达式中的顶级 InqR,尽管它没有坏处。您可能也不关心 DetR,但假设您这样做,您想说“给我具有以下规范的 Refinfo 元素的父级”

DetRs/Refinfo[RefType='Id' and RefId='0']/..
于 2008-12-13T23:53:29.937 回答