如何在 JSDOM 中使用 XPath 获取 XML 属性

问题描述 投票:0回答:3
javascript node.js xml xpath jsdom
3个回答
0
投票

也许可以尝试一下

const dateEl = doc.evaluate("PARAGRAPH/DATE", doc, null, 9, null).singleNodeValue;

const date = dateEl.getAttribute("ISO");

0
投票

用科迈罗试试这个。在这里使用 runkit 进行测试 https://runkit.com/embed/vxenbgqb1g7x

var { transform } = require("camaro")

var xml = `
<PARAGRAPH>
    This is a text, about something, that happened on 
    <DATE ISO="20131117">17.November 2013</DATE>
    . It is a very important date.
</PARAGRAPH>
`;

async function main() {
    console.log(await transform(xml, {iso: 'PARAGRAPH/DATE/@ISO'}))
}

main()

输出

{iso: "20131117"}

0
投票

不幸的是,解决方案https://stackoverflow.com/a/70065663/5871883它不起作用,因为https://github.com/jsdom/jsdom/issues/3293

我的错误:

[Nest] 39338 - 10/24/2023, 9:13:58 PM 错误 [ExceptionsHandler] XPathResult 未定义

ReferenceError:XPathResult 未定义 在 processTicksAndRejections (节点:内部/进程/task_queues:95:5) 在异步/Users/livvius/IdeaProjects/renectere/liveeing/node_modules/@nestjs/core/router/router-execution-context.js:46:28 在异步/Users/livvius/IdeaProjects/renectere/liveeing/node_modules/@nestjs/core/router/router-proxy.js:9:17

© www.soinside.com 2019 - 2024. All rights reserved.