首页 > 解决方案 > XML标记的XPath取决于文本中的位置?

问题描述

我正在寻找能够区分涉及相同标签的两个场景的 XPath 表达式。

场景 1: <include.text>出现在字符串的中间,例如:

<par>We are certain that <include.text><Reference/></include.text> will be a big hit.</par>

场景 2: <include.text>直接出现在另一个(任意)标签之后,例如:

<par><include.text><Reference/></include.text> will be a big hit.</par>

就我而言,我可以//include.text用于第一个场景,但我需要另一个 Xpath 表达式,它允许我区分它直接出现在开始标签之后的场景(par在这种情况下,它可以是任何东西)。通常,开始标签 ( par) 将是父级而不是兄弟级。我已经研究过ends-with(),等等,但我还不太擅长 XPath。

标签: xmlxpath

解决方案


对于这个 XML,

<r>
  <par>We are certain that <include.text><Reference/></include.text> will be a big hit.</par>
  <par><include.text><Reference/></include.text> will be a big hit.</par>
</r>

这个 XPath,

//include.text[not(preceding-sibling::node())]

将只选择第二个include.text,因为它没有前面的兄弟节点。


推荐阅读