簡體   English   中英

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

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

我正在從事一項涉及使用 XML 文件並需要使用 XPath using.mjs 文件解析該文件的工作任務。 我通常不使用.mjs 文件編寫代碼,我通常使用的數據是 JSON。 這是我們正在從 C# 轉換為 Node 的遺留應用程序,我們需要能夠解析 XML 文件。 要求如下:

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

我什至不知道從哪里開始。 任何能指引我正確方向的幫助都會非常有幫助。 先感謝您。

舉一個非常簡單的例子,在 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));

文檔位於https://www.saxonica.com/saxon-js/documentation2/index.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM