xpath:检查<title>是否在第一个&lt; section&gt; of&lt; body&gt; </title>

时间:2014-03-18 10:47:52

标签: xpath

我有一个具有以下结构的XML:

<document>
<body>
<section>
<section>
<title>This is a title (this is the element we want to highlight)</title>
<p>This is some content</p>
</section>
<section>
<title>Another title</title>
<p>More content</p>
</section>
</section>
<section>
<section>
<title>Another title</title>
<p>This is some content</p>
</section>
<section>
<title>Another title</title>
<p>More content</p>
</section>
</section>
</body>
</document>

我试图找出一个Xpath表达式,它标记了body(section / body / section [1] / section [1] / title)中第一节中的第一个标题但是我无法让它工作

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

看来你的xpath很好。但是,我怀疑是名称空间。请尝试以下方法:

*:document/*:body/*:section[1]/*:section[1]/*:title

答案 1 :(得分:0)

解决了它。如果有人需要类似的解决方案,这对我有用:

count(../../preceding-sibling::node())+1=1