我正在使用 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>
如何仅选择具有 RefType=Id
和 RefId=0
的 DetRs
,即上面的“第一个” ?
我尝试了几种不同的尝试,其中包括:
InqRs/DetRs[RefInfo/RefType='Id' and RefInfo/RefId='0']
InqRs/DetRs[RefInfo/RefType='Id'][RefInfo/RefId='0']
但是这些选择了两个 DetRs 部分(因为 StopNum 我认为 RefId 为 0)。
最佳答案
您想要顶部元素的所有 DetRs
子元素:
/*/DetRs
有一个 RefInfo
子项:
/*/DetRs
[引用信息]
RefType
的值为“Id
”:
/*/DetRs
[RefInfo
[RefType
='Id
']
]
它的 RefId
值为 0:
/*/DetRs
[RefInfo
[RefType
='Id
'
和
RefId
=0
]
]
这个 XPath 表达式正确地选择了所提供的 XML 文档中所需的第一个 DetRs
元素。
当然,如果有人有其他风格偏好,上面的表达式也可以写成:
/*/DetRs[RefInfo[RefType='Id' and RefId=0]]
关于xml - Xpath兄弟条件测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/365750/