Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a node-set in a JavaScript XPath query

I have a real simple question that I can't seem to find an answer to.

I want to compress two XPath statements (that are getting attribute values). I learned about the | operator, hearing how it returns node sets.

var getdata = xmldoc.evaluate
(
    '/foo/bar[@world=\''+hello+'\']/child::*/attribute::name
    |/foo/bar[@world=\''hello+'\']/child::*/attribute::id', 
    xmldoc, null, XPathResult.ANY_TYPE, null
);

To anyone wondering, no I do not format my evaluation strings that way ... though, I sort of like it now that I typed it out. Anyways, this is how I tested it out.

alert(getItemData.iterateNext().childNodes[0].nodeValue);

That works! But it only returns the first one. While writing this, I just tried .length and made a break through ... it's only counting one item. Was I deceived about the concept of |? How can I get a set and then go through them?

XML document, as requested.

<?xml version="1.0" encoding="ISO-8859-1"?>
<foo>
    <bar world="hello" id="1">
        <subbar name="item1" id="2">
        </subbar>
    </bar>
    <bar world="bye" id="3">
        <subbar name="item2" id="4">
        </subbar>
    </bar>
</foo>

Edit: I am currently using a function that grabs the element rather than the attribute, but I would really like to know the other way. Unless what I am doing is the best way.

like image 672
Tarik Avatar asked Oct 28 '10 05:10

Tarik


People also ask

Can we use XPath in JavaScript?

This document describes the interface for using XPath in JavaScript internally, in extensions, and from websites. Mozilla implements a fair amount of the DOM 3 XPath, which means that XPath expressions can be run against both HTML and XML documents.

Can you use XPath expression used to select the target node and its values?

XPath assertion uses XPath expression to select the target node and its values. It compares the result of an XPath expression to an expected value. XPath is an XML query language for selecting nodes from an XML. Step 1 − After clicking Add Assertion, select Assertion Category – Property Content.

Is there a way to get element by XPath using JavaScript in Selenium Webdriver?

To identify a WebElement using xpath and javascript you have to use the evaluate() method which evaluates an xpath expression and returns a result.


2 Answers

If JQuery is an option, it might be worth your while to check out their XML traversal library. A quick search pulled up an article here. I wrote up a very rough example of what the logic may look like after you import the xml document, which is explained in the link.

var hello = "foo";
$('bar[world=' + hello + '] > subbar').each(function () {
    // You'd want to save these values somewhere else, obviously.
    $(this).getAttribute(name);
    $(this).getAttribute(id);
});
like image 170
raynjamin Avatar answered Nov 07 '22 06:11

raynjamin


The key here is the XPathResult type you use.

I have implemented a working sample for the same. Please refer the code at http://jsbin.com/eneso3/5/edit

Basically you have to use Iterator as result type sot hat we can iterate through them to get the text. Refer Xpath reference mentioned on the working code sample page.

like image 1
Badri Narayan Manicka Avatar answered Nov 07 '22 05:11

Badri Narayan Manicka