xpath:检查是否<;标题>;在第一位<;第节>;属于<;车身>;

xpath:检查是否<;标题>;在第一位<;第节>;属于<;车身>;,xpath,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>

我有一个具有以下结构的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表达式,它标记正文中第一节(document/body/section[1]/section[1]/title)中第一节中的第一个标题,但我无法让它工作


有什么建议吗?

您的xpath似乎很好。但是,我怀疑存在名称空间。请尝试以下操作:

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

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

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

为什么您不能让它工作,它似乎工作得很好:有点不同:
(document/body/section/section//title)[1]
名称空间似乎很好。问题是我的xpath(至少在我的XML编辑器中)也标记了第二节((document/body/section[2]/section[2]/title)