2

Eu tenho um XML. Mas algo está acontecendo que não estou entendendo. Ao tentar utilizar o XPath para retornar os elementos Author, usando //Author (testei via http://www.freeformatter.com/xpath-tester.html) só está trazendo apenas os contidos na tag SearchBookResponse:

<author>Robert Ludlum</author>
<author>Douglas Adams</author>
<author>Dr. Seuss (Theodor Seuss Geisel)</author>

Como faço pra que ele reconheça também os contidos dentro da tag searchForBooksReturn?

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
       <LibrarySearchResponse xmlns="urn:soft.librarysearch" xmlns:tns="urn:soft.librarysearch">
           <tns:query />
           <tns:books xmlns:nl="http://library.be">
               <searchBooksResponse xmlns="urn:soft.vub.ac.be/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                   <book xmlns="">
                       <author>Robert Ludlum</author>
                       <year>2004</year>
                       <isbn>95248457</isbn>
                       <language>fr</language>
                       <publisher>Orion</publisher>
                       <title>La Mémoire dans la peau (SOFT Library)    </title>
                </book>
                <book xmlns="">
                    <author>Douglas Adams</author>
                    <year>2002</year>
                    <isbn>60184547</isbn>
                    <language>fr</language>
                    <publisher>Del Rey</publisher>
                    <title>Le Guide du Voyageur Galactique (SOFT Library)</title>
                </book>
                <book xmlns="">
                    <author>Dr. Seuss (Theodor Seuss Geisel)</author>
                    <year>2008</year>
                    <isbn>606559890</isbn>
                    <language>fr</language>
                    <publisher>Amulet Books</publisher>
                    <title>Horton Entend un Zou! (SOFT Library)</title>
                </book>
            </searchBooksResponse>
            <searchForBooksReturn xmlns="http://library.be" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <author>James, E. L.</author>
                <date>3914-01-10T23:00:00.000Z</date>
                <isbn>0345803485</isbn>
                <language>English</language>
                <publisher>Vintage Books</publisher>
                <title>50 Shades of Grey (National Library)</title>
            </searchForBooksReturn>
            <searchForBooksReturn xmlns="http://library.be" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <author>James Dashner</author>
                <date>3914-04-20T22:00:00.000Z</date>
                <isbn>0385388896</isbn>
                <language>English</language>
                <publisher>The Maze Runner Series</publisher>
                <title>The Maze Runner Series (National Library)</title>
            </searchForBooksReturn>
            <searchForBooksReturn xmlns="http://library.be" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <author>William Sleator</author>
                <date>3908-03-31T22:00:00.000Z</date>
                <isbn>0810993562</isbn>
                <language>English</language>
                <publisher>Amulet Books</publisher>
                <title>Test (National Library)</title>
            </searchForBooksReturn>
            <searchForBooksReturn xmlns="http://library.be" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <author>Michalewicz, Zbigniew  Fogel, David B.</author>
                <date>3912-10-31T23:00:00.000Z</date>
                <isbn>9783642061349</isbn>
                <language>English</language>
                <publisher>Del Rey</publisher>
                <title>How to Solve It: Modern Heuristics (National Library)</title>
            </searchForBooksReturn>
            <searchForBooksReturn xmlns="http://library.be" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <author>Ludlum, R</author>
                <date>3913-10-31T23:00:00.000Z</date>
                <isbn>9783642061149</isbn>
                <language>English</language>
                <publisher>Del Rey</publisher>
                <title>The Matarese Circle (National Library)</title>
            </searchForBooksReturn>
            <searchForBooksReturn xmlns="http://library.be" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <author>Ludlum, R</author>
                <date>3911-10-31T23:00:00.000Z</date>
                <isbn>9783642461149</isbn>
                <language>English</language>
                <publisher>Del Rey</publisher>
                <title>The Parsifal Mosaic (National Library)</title>
            </searchForBooksReturn>
        </tns:books>
    </LibrarySearchResponse>
</soapenv:Body>
</soapenv:Envelope>

Agradeço desde já, valeu!

1 Resposta 1

1

O XPath não está a funcionar porque está a ser utilizado namespaces XML (os namespaces servem para resolver conflitos de nomes entre tags HTML). Para resolveres a situação, o que podes fazer, é escrever a seguinte expressão:

//*[name()='author']

Recorrendo à função name() és capaz de selecionar todos os elementos "<author>" equivalentes a "</author>".

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .