XPath - 根据多个子节点选择选择父节点

4
让我们假设有一个像这样的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>

我们要如何选择具有 名称 属性和至少一个非空的 译者 节点的书架节点?
基本的XPath教程没有提供这么复杂的例子。
1个回答

9
您可以将条件链接在一起:
//bookshelf[cathegory/@name][.//translator/text()]

它选择一个书架,该书架有一个具有名称属性的类别子元素,并且有一个非空的翻译器后代。

无法评估断言,可能是因为Python对XPath的支持有限。无论如何感谢。 - user707779
使用[cathegory/@name and .//translator/text()]怎么样? - choroba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接