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

时间:2021-02-21 22:17:22

How can I use XPath to select an XML-node based on its content?

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

If I e.g. have the following xml and I want to select the <author>-node that contains Ritchie to get the author's full name:

如果我有以下xml,我想选择 -node,其中包含Ritchie,以获得作者的全名:

<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>

3 个解决方案

#1


23  

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

or

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

#2


4  

The XPath for this is:

这里的XPath是:

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

In C# the following code would return "Ritchie, Dennis M.":

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

xmlDoc.SelectSingleNode("/books/book/authors/author[contains(., 'Ritchie')]").InnerText;

#3


4  

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

#1


23  

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

or

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

#2


4  

The XPath for this is:

这里的XPath是:

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

In C# the following code would return "Ritchie, Dennis M.":

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

xmlDoc.SelectSingleNode("/books/book/authors/author[contains(., 'Ritchie')]").InnerText;

#3


4  

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