简体   繁体   English

在 Node JS 中运行 SaxonJS 时出错,“'/' 的上下文项必须是节点”

[英]Error when running SaxonJS in Node JS, "Context item for '/' must be a node"

const node = document.createElement('div');
node.innerHTML = "<p>Text 1</p>"

For this JS HTMLElement, how should I run an XPath against it using SaxonJS?对于这个 JS HTMLElement,我应该如何使用 SaxonJS 对它运行 XPath?

const res = saxonJs.XPath.evaluate('//p', node);

Has error Context item for '/' must be a node有错误Context item for '/' must be a node

const res = saxonJs.XPath.evaluate('*', node);

Has error Context item for child axis is not a node - supplied:HashTrie map{}有错误Context item for child axis is not a node - supplied:HashTrie map{}

MORE DETAILS : The ultimate goal is to run XPath 3.0 against an HTML DOM (parsed from a string) in Node JS, so I'm open to using other modules or methods更多细节:最终目标是在 Node JS 中针对 HTML DOM(从字符串解析)运行 XPath 3.0,所以我愿意使用其他模块或方法

Inside the browser there is always one sole DOM implementation that both SaxonJS and the browser use so there you can indeed use HTML DOM document and nodes as context items, even if not officially supported in eg https://www.saxonica.com/saxon-js/documentation2/index.html#!api/getResource (by adding html as a possible type ).在浏览器内部,SaxonJS 和浏览器始终使用一个唯一的 DOM 实现,因此您确实可以使用 HTML DOM 文档和节点作为上下文项,即使在例如https://www.saxonica.com/saxon中没有官方支持也是如此-js/documentation2/index.html#!api/getResource (通过添加html作为可能的type )。

 const html = `<p>This is a test. <p>This is a test. <ul> <li>a</li> <li>1</li> <li>b</li> </ul>`; const htmlDoc = new DOMParser().parseFromString(html, 'text/html'); const alphabeticLIElements = SaxonJS.XPath.evaluate("//li[matches(., '\\p{L}+')]", htmlDoc, { xpathDefaultNamespace: 'http://www.w3.org/1999/xhtml' }); console.log(alphabeticLIElements);
 <script src="https://www.saxonica.com/saxon-js/documentation2/SaxonJS/SaxonJS2.rt.js"></script>

Inside Node.js, things, as far as I understand, are different, the SaxonJS internalizes some third party DOM implementation (which by now might be compatible to support text/html parsing) but doesn't expose any method or way for HTML parsing.在 Node.js 内部,据我所知,情况有所不同,SaxonJS 内部化了一些第三方 DOM 实现(现在可能兼容以支持text/html解析),但没有公开任何用于 HTML 解析的方法或方式。

I did a feature request https://saxonica.plan.io/issues/5302 (or two) on that some while ago but so far there has been no new release on the 2.x branch to implement or expose that.不久前,我做了一个功能请求https://saxonica.plan.io/issues/5302 (或两个),但到目前为止,2.x 分支上还没有新版本来实现或公开它。

So I afraid, unless you find a way through the Node.js module structure to access the internalized DOM implementation, there is currently no way to achieve that.所以我担心,除非你找到通过 Node.js 模块结构访问内部化 DOM 实现的方法,否则目前没有办法实现。

The official way, once implemented/exposed, I think would be along the lines of官方方式,一旦实施/公开,我认为将遵循

const SaxonJS = require("saxon-js");

const html = `<p>This is a test.
<p>This is a test.
<ul>
  <li>a</li>
  <li>1</li>
  <li>b</li>
</ul>`;

SaxonJS.getResource({ type : 'html', text: html }).then(htmlDoc => {

console.log(htmlDoc);

const alphabeticLIElements = SaxonJS.XPath.evaluate("//li[matches(., '\\p{L}+')]", htmlDoc, { xpathDefaultNamespace : 'http://www.w3.org/1999/xhtml' });

console.log(alphabeticLIElements);
}).catch(err => console.log(err));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM