开发者

XPath select tags by id not descendants of

I have the following code and I have to 开发者_如何学运维select all the nodes with id="text" but not the nodes that already have a parent with id="text":

<fo:block id="text">
  test00
  <fo:block id="text">
      test01
  </fo:block>
  <fo:block>
      test02
  </fo:block>
</fo:block>
<fo:block id="text">
    test03
</fo:block>

so in this example the query have to return only the two fo:block with content test00 and test03.

Thank you.


I'd go with something like this:

//fo:block[@id='text' and not(./*[@id='text'])]

I'm going to give it a test right now to make sure it's sane. Yeah. It returns text00 and text03, as required. So allow me to explain this expression.

//fo:block             # Select all fo:block elements in the document
[
  @id='text' and       # Get only those whose id attribute's value is "text"
  not(./*[@id='text']) # and whose children do not have id attributes equal to "text"
]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜