How to get attribute and text from xpath

Please can you tell me what is the syntax for getting the attributes and text once an xpath query has returned a set of nodes.

The example in Ajax xpath documentation shows nodename. I need the same for attributes of node and text.


Greetings, Purvez,

I’ve only been using DHTMLX a few weeks, but this code seems to work for displaying the text from XPath:

var result = loader.doXPath("/ArrayOfRequest/Request/AutoNumber"); //alert("result length: " + result.length); for(var i=0;i<result.length;i++){ alert(result[i].childNodes(0).nodeValue); }

Remember, the value of a node (its text, in other words) is actually a child node of that node.

Kind regards.

Hi Ajaxian

Thanks very much for your reply it is very helpful. Have you any idea about how to access attributes of nodes?


You’re welcome, Purvez. Yes, to access attribute values, please try this:

var result = loader.doXPath("/ArrayOfRequest/Request/AutoNumber@Timestamp");

It would return all attributes called ‘Timestamp’ associated with the node ‘AutoNumber’. Remember, ‘@’ is for ‘attribute’.

Kind regards.

Thanks Ajaxian, you are a star!

Presumably once I’m iterating through a particular node I could just do .@attribute or is it ./@attribute?

Thanks again for your help.

Greetings again, Purvez,

To get all the attributes of a current node, the syntax is:


To get a single named attribute in the current node, it is:


Kind regards

Ajaxian you rock!! Thanks for that info. It helped a LOT!




Please can somebody tell me how to access the ‘attributes’ once I’ve got the ‘results’ back as an array from the xpath query?
e.g. What do I put in place of the ??? in the following code.

results = loader.doXPath("//SomeExpr");
				for(var i=0; i<results.length; i++)


 //To access attribute 'Test' within results[i]

Thanks for your help

To get atributes by index:
var att = result[i].attributes[0].text;

To get elements by index:
var elem = result[i].childNodes(0).text;