How to do an Xpath expression in an XML with multiple namespaces?

Asked

Viewed 124 times

3

I have the following xml:

<root xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <body xmlns="http://www.foo.com/bar">
        <checkCredit>
            <CheckCreditOut>
                <nomeSerasa />
                <addresses>
                    <id>9-CAZ7QCU</id>
                    <decision>ACCEPT</decision>
                </addresses>
            </CheckCreditOut>
        </checkCredit>
    </body>
</root>

I want to validate the element value Decision, for this I use the expression:

string(//decision) = 'ACCEPT'

And the expression works, if I remove some of the namespaces from the document, the way it is, the expression always returns false because it cannot reach the value of the element.

How do I correct the expression to work with 2 or more namespaces?

P.S.: I have also tested using Xpath /root/body/checkCredit/CheckCreditOut/addresses/decision

1 answer

0

You can use the function local-name xpath:

/*[local-name() = 'checkCredit']/*[local-name() = 'CheckCreditOut']/*[local-name() = 'addresses']/*[local-name() = 'decision']
  • Hello, Ulysses, thanks for the help, I tested the function and the problem continues, the expression does not seem to find the element (returns String='').

  • I had misspelled and just edited the answer. Did when you tested it was not yet with the error? The error was that the first local-name was with the value 'video', which does not have in its xml.

Browser other questions tagged

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