Evaluating an XPath with document.evaluate() to get an array of nodes The Problem Statement:


  Filter all nodes with an existing attribute that starts with a specific string (temp for example purposes). Print an array of node string representations as a result.


The Code:



var result = document.evaluate("//*[@*[starts-with(name(), 'temp')]]", document, null, XPathResult.ANY_TYPE, null);

var node = result.iterateNext(); 
var nodes = [];
while (node) {
  nodes.push(node);
  node = result.iterateNext();
}
console.log(nodes);
&lt;div temp-callback="1" temp-post-callback="4"&gt;&lt;/div&gt;
&lt;div temp-callback="2"&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div temp-callback="8" temp-post-callback="7"&gt;&lt;/div&gt;




The code is a bit bulky for such a straightforward problem. Is there a simpler and a more concise way to get an array of nodes from a document.evaluate() result?
I don't see a method to directly turn it into an array -- Array.from doesn't work, eg.  The best I could achieve was a little cleanup:



var result = document.evaluate("//*[@*[starts-with(name(), 'temp')]]", document, null, XPathResult.ANY_TYPE, null);

var node, nodes = []
while (node = result.iterateNext())
  nodes.push(node);

console.log(nodes);
&lt;div temp-callback="1" temp-post-callback="4"&gt;&lt;/div&gt;
&lt;div temp-callback="2"&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div temp-callback="8" temp-post-callback="7"&gt;&lt;/div&gt;