DHTMLX Docs & Samples Explorer

XPath with dhtmlxAjax

XPath is a language for addressing parts of an XML document. Complete W3C Specification can be found here. dhtmlxAjax supports XPath for loader object (it comes to the callback function as an incomming argument in case of async request or you get it as a result of getSync and postSync methods). Common way of usage is:

var loader = dhtmlxAjax.getSync(url);
loader.doXPath(xpathExp, docObj, namespace, result_type);

where:

  • xpathExp - XPath expression, like ”/itemsitem[@id='123']”;
  • docObj - XML document object. In case it is null XPath expression will be processed against XML document which came in response;
  • namespace - namespace to take into account if any. Use null if no namespace required;
  • result_type - by default result will be an array of found elements, but if you set last parameter to “single”, the first found element will be returned.

Thus in the most simple cases the following syntax is enough. It will return array of found elements. You can iterate through them using common way of iterating through array.

var result = loader.doXPath("/some/expression[@arg='value']");
for(var i=0;i<result.length;i++){ 
alert(result[i].nodeName)
 }

In case you use namespace:

  <node xmlns="http://...">
    <subnode/>
    <subnode/>
  </node>
  var result = loader.doXPath("//na:subnode",null,"http://...");