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

Yur*_*sov 4 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>
Run Code Online (Sandbox Code Playgroud)

我们如何选择书架具有cathegory与节点的名称属性和至少一个与非空的转换节点?

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

cho*_*oba 8

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

//bookshelf[cathegory/@name][.//translator/text()]
Run Code Online (Sandbox Code Playgroud)

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