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

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

<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>
有帮助吗?

解决方案

/books/book/authors/author[contains(., 'Ritchie')]

或者

//author[contains(., 'Ritchie')]

其他提示

其 XPath 是:

/books/book/authors/author[contains(., 'Ritchie')]

在 C# 中,以下代码将返回“Ritchie, Dennis M.”:

xmlDoc.SelectSingleNode("/books/book/authors/author[contains(., 'Ritchie')]").InnerText;
//author[contains(text(), 'Ritchie')]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top