Search code examples
javascriptsaxon-js

Iteration through a Saxon-JS sequence


This javascript (hosted on a browser console) ...

var xmlDoc = jQuery.parseXML("<foo>Stuff</foo>");
for (let item of SaxonJS.XPath.evaluate( '/foo/text()', xmlDoc,
                 {resultForm:'iterator'})) {console.log(item);}

... returns error ...

SaxonJS.XPath.evaluate(...) is not a function or its return value is not iterable

... instead of the expected output ...

"Stuff"

Why?

According to documentation here, the evaluate() expression should return an iterator. But it does not.

Libraries used include:

  1. jQuery; and
  2. Saxon-js javascript library.

Update

I can get a functionally correct result with this alternative expression ...

for (let item of SaxonJS.XPath.evaluate('string(foo/text())',xmlDoc, 
  {resultForm:'array'})) {console.log(item)}

... but I really want to use a lazy iterator, if that is possible, rather than an array.


Solution

  • Ok, I think I am just invoking iterator wrong. This works ...

    SaxonJS.XPath.evaluate( '/foo/text()', xmlDoc,
      {resultForm:'iterator'}).forEachItem( function( node){
        console.log( node)})