使用XPath选择匹配的兄弟姐妹的前一个兄弟?

时间:2016-04-21 01:58:13

标签: xml xslt xpath

我有以下代码:

<include>
  <fallback>
    <title>Content</title>
  </fallback>
</include>
<include>
  <fallback>
    <title>Content</title>
  </fallback>
</include>
<include> <-- I want this.
  <fallback>
    <title>Content</title>
  </fallback>
</include>
<adhoc>
  <title>Content</title>
</adhoc>
<include>
  <fallback>
    <title>Content</title>
  </fallback>
</include>

我需要找到第三个<include>,因为它有<adhoc>作为其直接跟随兄弟。有没有用XPath找到这个?

1 个答案:

答案 0 :(得分:4)

  

&#34;我需要找到第三个<include>,因为它有<adhoc>,因为它直接跟随兄弟。&#34;

此要求可以转换为XPath,如下所示:

//include[following-sibling::*[1][self::adhoc]]