简体   繁体   English

如何在 Node.js 中使用带有 ESM 的 XPath 解析 XML 文件

[英]How to parse XML files using XPath with ESM in Node.js

I am working on a task for work that involves using an XML file and needing to parse that file with XPath using.mjs files.我正在从事一项涉及使用 XML 文件并需要使用 XPath using.mjs 文件解析该文件的工作任务。 I don't normally code using.mjs files and the data that I am typically working with is JSON.我通常不使用.mjs 文件编写代码,我通常使用的数据是 JSON。 This is a legacy app that we are converting from C# to Node and we need to be able to parse XML files.这是我们正在从 C# 转换为 Node 的遗留应用程序,我们需要能够解析 XML 文件。 The requirements are as follows:要求如下:

Must use.mjs extension Module must run in Node.js so it should not depend on any browser-specific JavaScript features Module's default export should be an asynchronous function which crawls the directory tree and parses the XML files it finds Must use.mjs extension Module must run in Node.js so it should not depend on any browser-specific JavaScript features Module's default export should be an asynchronous function which crawls the directory tree and parses the XML files it finds

I am at a loss for even where to begin.我什至不知道从哪里开始。 Any assistance with pointing me in the right direction would be extremely helpful.任何能指引我正确方向的帮助都会非常有帮助。 Thank you in advance.先感谢您。

To give you a very simple example using XPath 3.1 with SaxonJS in an ESM module:举一个非常简单的例子,在 ESM 模块中使用 XPath 3.1 和SaxonJS

import { default as SaxonJS } from 'saxon-js';

const xml = `<root>
  <item>a</item>
  <item>b</item>
  <item>c</item>
</root>`;

const xmlDoc = await SaxonJS.getResource({ type: 'xml', text: xml });

console.log(SaxonJS.XPath.evaluate(`random-number-generator(current-dateTime())?permute(/root/item) => serialize(map { 'method' : 'xml', 'indent' : true() })`, xmlDoc));

Documentation is at https://www.saxonica.com/saxon-js/documentation2/index.html文档位于https://www.saxonica.com/saxon-js/documentation2/index.html

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

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