Domanda

Come posso utilizzare XPath per selezionare un nodo XML in base al suo contenuto?

Se ad es.ho il seguente xml e voglio selezionare il nodo <author> che contiene Ritchie per ottenere il nome completo dell'autore:

<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>
È stato utile?

Soluzione

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

O

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

Altri suggerimenti

L'XPath per questo è:

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

In C# il codice seguente restituirebbe "Ritchie, Dennis M.":

xmlDoc.SelectSingleNode("/books/book/authors/author[contains(., 'Ritchie')]").InnerText;
//author[contains(text(), 'Ritchie')]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top