Bookamp
Bookamp

Reputation: 682

problems with xpath evaluate with namespaces in javascript

I'm trying to evaluate a simple xpath expression.

I have an xml object,

var xn = response.resultXML; 

xn.outerHTML evaluates to:

<Result xmlns="http://url.com/services">
<AE>0</AE>
<FM>0</FM>
<OCX>0</OCX>
<TX>0</TX>
<AR>0</AR>
<Items>
    <RI>
        <PR>1</PR>
        <Date>2003-07-19T00:00:00</Date>
        <AR>228217.5600</AR>
        <AQ>19018.1300</AQ>
        <CBZ>13.519997630331753554502369668</CBZ>
        <XN>1.1266664691943127962085308057</XN>
        <AM>19018.1300</AM>
        <Unit>PerMonth</Unit>
        <UnitString>$/Month</UnitString>
        <DD>0.0</DD>
        <CR>0</CR>
    </RI>
</Items>
</Result>

I'm trying to evaluate this xml using the following query. The xml is the result of a soap webservice call.

var xdoc = xn.ownerDocument;
resolver = xdoc.createNSResolver(xdoc.documentElement);
var es = xdoc.evaluate("Items", xn, resolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
var node = es.singleNodeValue

I do not get any results. Can somebody point me in the right direction to retrieve the Items node? Thanks.

EDIT -

This is the outer HTML for xn.ownerDocument.documentElement :

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
    <Result xmlns="http://url.com/services">
        <AE>0</AE>
        <FM>0</FM>
        <OCX>0</OCX>
        <TX>0</TX>
        <AR>0</AR>
        <Items>
            <RI>
                <PR>1</PR>
                <Date>2003-07-19T00:00:00</Date>
                <AR>228217.5600</AR>
                <AQ>19018.1300</AQ>
                <CBZ>13.519997630331753554502369668</CBZ>
                <XN>1.1266664691943127962085308057</XN>
                <AM>19018.1300</AM>
                <Unit>PerMonth</Unit>
                <UnitString>$/Month</UnitString>
                <DD>0.0</DD>
                <CR>0</CR>
            </RI>
        </Items>
    </Result>
</soap:Body>
</soap:Envelope>

Upvotes: 2

Views: 1671

Answers (2)

Bookamp
Bookamp

Reputation: 682

Thanks Martin Honnen - Since I do not know what default namespace I might have in my xml node, I modified his answer slightly. This now works for me.

var resolver = null;
var ns = (new window.DOMParser).parseFromString(xn.outerHTML, "text/xml").children[0].getAttribute("xmlns");
        if (ns)
        {
            resolver = function()
            {
                return ns;
            }
            xpath = "defaultNamespace:" + xpath;
        }

var es = xdoc.evaluate(xpath, xn, resolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167571

If you know the namespace of the Items elements in advance then simply do

var es = xdoc.evaluate("//df:Items", xn, function(prefix) { if (prefix === 'df') { return 'http://url.com/services'; } else { return null; }}, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

Upvotes: 2

Related Questions