如何根据其内容选择XML节点?

Cro*_*ros 16 xml xpath

如何使用XPath根据其内容选择XML节点?

如果我有以下xml,我想选择包含Ritchie的<author> -node来获取作者的全名:

<books>
    <book isbn='0131103628'>
        <title>The C Programming Language</title>
        <authors>
            <author>Ritchie, Dennis M.</author>
            <author>Kernighan, Brian W.</author>
        </authors>
    </book>
    <book isbn='1590593898'>
        <title>Joel on Software</title>
        <authors>
            <author>Spolsky, Joel</author>
        </authors>
    </book>
</books>
Run Code Online (Sandbox Code Playgroud)

aku*_*aku 23

/books/book/authors/author[contains(., 'Ritchie')]
Run Code Online (Sandbox Code Playgroud)

要么

//author[contains(., 'Ritchie')]
Run Code Online (Sandbox Code Playgroud)

  • 对于像我这样的XPath新手,可能有必要注意`.`是`text()`的一个较短的别名 - 所以是的,这个答案是正确的. (5认同)