我试图使用saxon来评估一些xPaths
我正在运行以下代码
const sourceCode= " ... " //a string representing the code source of https://www.imdb.com/chart/boxoffice
const doc = new DOMParser({errorHandler: () => {} }).parseFromString(sourceCode, 'text/html')
try {
const res = saxon.XPath.evaluate(selectors[0].path, doc, { xpathDefaultNamespace : 'http://www.w3.org/1999/xhtml' })
console.log("saxon res", res);
} catch(e) {
console.log("saxon e", e);
}将引发以下错误
saxon e RangeError: Maximum call stack size exceeded
at Hb.Cb (/application/node_modules/saxon-js/SaxonJS2N.js:3904:409)
at new Hb (/application/node_modules/saxon-js/SaxonJS2N.js:3905:286)
at Object.fromString (/application/node_modules/saxon-js/SaxonJS2N.js:3948:119)
at H (/application/node_modules/saxon-js/SaxonJS2N.js:4377:39)
at S (/application/node_modules/saxon-js/SaxonJS2N.js:4378:208)
at H (/application/node_modules/saxon-js/SaxonJS2N.js:4378:500)
at /application/node_modules/saxon-js/SaxonJS2N.js:4379:103
at Array.forEach (<anonymous>)
at R (/application/node_modules/saxon-js/SaxonJS2N.js:4379:79)
at Y (/application/node_modules/saxon-js/SaxonJS2N.js:4379:196)
at H (/application/node_modules/saxon-js/SaxonJS2N.js:4379:28)
etc...我尝试过的任何网页都有相同的错误。如何解析源代码并使用saxon-js计算页面。
注意:我可以选择使用XPath3.0的Note2:上面的代码可以很好地使用npm,但是它使用XPath1
发布于 2022-02-10 09:37:48
在浏览器中,saxon使用浏览器的DOM实现并与例如new DOMParser().parseFromString('...', 'text/html')进行良好的互操作,但我认为在Node.js下,它基于“第三方组件,包括sax、xmldom和Big”集成了自己的DOM,“集成到saxon代码库中”。
我认为他们还没有集成一个文本/ HTML解析器,所以您可以使用SaxonJS.getResource解析X(HT)ML,但到目前为止还没有。
XML示例:
const SaxonJS = require("saxon-js");
const htmlInput = `<html><body><h1>Test</h1>
<p>This is p 1.</p>
<p>This is p2.</p>
</body>
</html>`;
SaxonJS.getResource({ text : htmlInput, type : 'xml' }).then(doc => {
const result = SaxonJS.XPath.evaluate(`//p/string()`, doc);
console.log(result);
});https://stackoverflow.com/questions/71058271
复制相似问题