2

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>

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.