0

我正在尝试使用 saxon JS 来评估一些 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 评估页面

注意:我对使用 xpath 3.0 的替代方案持开放态度 注意2:上面的代码适用于 npm xpath 但它使用 xpath 1

4

1 回答 1

0

在浏览器中,Saxon-JS 使用浏览器的 DOM 实现并与 eg 互操作良好,new DOMParser().parseFromString('...', 'text/html')但我认为在 Node.js 下集成了自己的 DOM,基于“第三方组件,包括 sax-js、xmldom 和 Big”,被“集成到saxon-js 代码库”。

我认为他们没有集成 text/html 解析器,因此您可以使用 SaxonJS.getResource 解析 X(HT)ML,但到目前为止还不能解析 HTML。

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);
});
于 2022-02-10T09:37:48.110 回答