XPath - 基于多个子选择选择父节点

时间:2013-04-02 10:30:34

标签: xpath

我们让XML看起来像那样

<bookshelf>
  <cathegory name = "Programming" />
  <book name = "Tille 1" >
    <author>....</author>
  </book>
  <book name = "Tille 2" >
    <author>....</author>
    <translator></translator>
  </book>
  <book name = "Tille 3" >
    <author>....</author>
    <translator>John D.</translator> <!-- non-empty nodes are acceptred! -->
  </book>
</bookshelf>

我们如何选择书架 名称属性 图书属性 -empty 翻译节点?

基本的XPath教程没有提供如此复杂的示例。

1 个答案:

答案 0 :(得分:10)

您可以一个接一个地链接条件:

//bookshelf[cathegory/@name][.//translator/text()]

它选择了一个书架,其中有一个带有name属性的导弹子,并且它是一个非空翻译器后代的散列。