Xpath does not recognize XML elements

Asked

Viewed 81 times

2

I have an XML. But something is happening that I don’t understand. When trying to use Xpath to return the Author elements, using //Author (tested pathway http://www.freeformatter.com/xpath-tester.html) is only bringing only those contained in the tag SearchBookResponse:

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

How do I make him recognize also those contained within the 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>

Thanks in advance, thanks!

1 answer

1

Xpath is not working because XML namespaces are being used (namespaces are for solving name conflicts between HTML tags). To resolve the situation, what you can do is write the following expression:

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

Using the function name() you are able to select all elements "<author>" equivalent to "</author>".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.