1

I think I set up the namespace resolver right,

var nodes=xml.evaluate(path, //xpathExpression
                               xml,  //contextNode
                               NSResolver, //namespaceResolver
                               XPathResult.ANY_TYPE, //resultType
                               null //result
                              );

and I think I am setting path correctly, (Ive tried alot of variation here, pretty much anything that might work)

path="/";

but the nodes.iterateNext() seems to be telling me I did something wrong :

firebug output : nodes : [object XPathResult] length : undefined 

the x and xml object are good though, because I can see them and the xml in firebug. If you use my code, Im just testing in chrome so if youre using IE you may have a whole other can of worms. :)

Heres the xml (sanitized version)

<dataset xmlns="http://stub.test.data1" xmlns:xs="http://another.stub.test.moredata">
<!--
<dataset
    xmlns="http://stub.test.data1"
    xmlns:xs="http://another.stub.test.moredata"
    xs:schemaLocation="http://yet.more.stub.test.data/xmldata.xsd"
>
-->
    <metadata>
        <item name="a" type="xs:string" length="92"/>
        <item name="b" type="xs:string" length="50"/>
        <item name="c" type="xs:short" precision="1"/>
        <item name="d" type="xs:string" length="66"/>
        <item name="e" type="xs:string" length="26"/>
        <item name="f" type="xs:string" length="6"/>
        <item name="g" type="xs:string" length="264"/>
        <item name="h" type="xs:double" precision="2"/>
        <item name="i" type="xs:string" length="22"/>
        <item name="j" type="xs:date"/>
        <item name="k" type="xs:date"/>
        <item name="l" type="xs:string" length="16"/>
        <item name="m" type="xs:short" precision="1"/>
        <item name="n" type="xs:short" precision="1"/>
        <item name="o" type="xs:string" length="50"/>
    </metadata>
    <data>
        <row>
            <value>someData1</value>
            <value>someData2</value>
            <value>someData3</value>
            <value>someData4</value>
            <value>someData5</value>
            <value>someData6</value>
            <value>someData7</value>
            <value>someData8</value>
            <value>someData9</value>
            <value>someData10</value>
            <value>someData11</value>
            <value>someData12</value>
            <value>someData13</value>
            <value>someData14</value>
            <value>someData15</value>
        </row>
    </data>
</dataset>

And heres the javascript :

function loadXMLDoc(dname)
{
    if (window.XMLHttpRequest)
      {
        xhttp=new XMLHttpRequest();
      }
    else
      {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }

    //initializes the request
    xhttp.open("GET",dname,false);//method, url, optional async defaults to true

    try {xhttp.responseType="msxml-document"} catch(err) {
        console.log('hey, an error occured');
    } // Helping IE

    xhttp.send("");//send the request. Does not return till the response is returned (due to false above)

    return xhttp;
}

function NSResolver(nsPrefix) {
    console.log("nsPrefix : " + nsPrefix);
    if(nsPrefix == "xs") {
        return "http://www.w3.org/2001/XMLSchema-instance";
    }
}

function displayNodes() {

    // code for IE
    if (window.ActiveXObject || xhttp.responseType=="msxml-document")
    {
        console.log('code for IE');
        console.log('path=' + path);
        xml.setProperty("SelectionLanguage","XPath");
        nodes=xml.selectNodes(path);
        for (i=0;i<nodes.length;i++)
          {
          document.write(nodes[i].childNodes[0].nodeValue);
          document.write("<br>");
          }
    }

    // code for Chrome, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
        console.log('code for chr / ff / op');
        console.log('path=' + path);


        //docs : http://help.dottoro.com/ljruhkuj.php
        var nodes=xml.evaluate(path, //xpathExpression
                               xml,  //contextNode
                               NSResolver, //namespaceResolver
                               XPathResult.ANY_TYPE, //resultType
                               null //result
                              );

        console.log("nodes : " + nodes + " length : " + nodes.length);
        var result=nodes.iterateNext();

        while (result)
          {
              document.write(result.childNodes[0].nodeValue);
              document.write("<br>");
              result=nodes.iterateNext();
          }
    }

    document.write('shit should have displayed by now');
}


path="/"; 

function reload() {
    x=loadXMLDoc("testxml.xml"); //x is now a XMLHttpRequest object
    xml=x.responseXML; //xml is now a response to the request, or null if it failed

    displayNodes();
    console.log("x (XMLHTTPRequest Object) : " + x);
    console.log("xml (XMLHTTPRequest.responseXML) : " + xml);
}

reload();

1 Answer 1

1

Looking at Mozilla's documentation for using XPath in Javascript, it seems that the XPathResult object has no such property as length that you're asking for.

So when firebug says length : undefined, that doesn't necessarily mean you've done something wrong in your XPath path or use of evaluate(). The only thing you've done wrong that I can see is to ask for nodes.length.

If your result were a snapshot, you could ask for nodes.snapshotLength, but it isn't:

When the result type in the resultType parameter is specified as ANY_TYPE, ... if the returned result type is a node-set then it will only be an UNORDERED_NODE_ITERATOR_TYPE.

Now when you iterate, you should get one result node: the document root node, that is, the (invisible) parent of the <dataset> element. Next, you're asking it to print result.childNodes[0].nodeValue. result.childNodes[0] should be the <dataset> element. The .nodeValue of an element is null, according to these docs. So presumably your document.write() is not showing anything.

Instead, try printing result.nodeName (docs here). This should give #document for the root node, or else the name of the element you've selected.

And if you're just trying to get something working, and verify that it's working, I would change your path to "/*". You'll get a more tangible result, namely, the <dataset> element.

Sign up to request clarification or add additional context in comments.

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.