我有xsl文件,我想应用于node.js中的一些XML内容。我找到了saxon-js,它似乎提供了我需要的东西。虽然我对文档有些迷茫,但在节点中运行transform之前,我似乎应该先将xsl文件转换为.sef文件。
saxon-js自述提到了其他程序包,xslt3应该能够这样做(见自述):
编译样式表 要将books.xsl中保存的样式表编译为books.sef.json中的SEF文件,要在浏览器或Node.js中运行,请使用命令行:
xslt3 -xsl:books.xsl -export:books.sef.json -t -ns:##html5
但是,在安装xslt3包后,没有可执行的可执行文件来运行转换。
我是不是遗漏了什么?如何将xsl转换为sef.json (一次可以,不需要从服务器动态地运行它)?
发布于 2021-03-12 02:24:40
FWIW,下面是您如何在不使用xslt3的情况下以编程方式完成此操作(以防其他人登陆这里,寻找基于代码的解决方案):
// xsl: path to XSLT file
// xml: path to XML file to be transformed
const saxon = require("saxon-js");
const env = saxon.getPlatform();
const doc = env.parseXmlFromString(env.readFile(xsl));
// hack: avoid error "Required cardinality of value of parameter $static-base-uri is exactly one; supplied value is empty"
doc._saxonBaseUri = "file:///";
const sef = saxon.compile(doc);
/* now you can use `sef`:
const resultStringXML = saxon.transform({
stylesheetInternal: sef,
sourceText: env.readFile(xml)
});
*/这有点像猴子补丁(尤指)。_saxonBaseUri部分);无论如何,在这里添加它,因为我(还没有)在任何文档中找到一种更好的/正式的方法-- Saxonica或其他。
发布于 2020-11-09 09:35:41
在项目中将包作为本地依赖项安装后,您可以在./node_modules/xslt3 3/xslt3.js中找到可执行文件。
npm i xslt3
❯ ./node_modules/xslt3/xslt3.js
Saxon requires a stylesheet as -xsl: argument
Error FORG0001
Cannot locate stylesheet ('/Users/<username>/Projects/saxonjs-poc/undefined')或者,您可以使用-g标志和npm全局安装xslt3。那么xslt3就可以作为可执行文件在您的机器上的任何地方使用。
npm i -g xslt3
❯ xslt3
Saxon requires a stylesheet as -xsl: argument
Error FORG0001
Cannot locate stylesheet ('/Users/<username>/Projects/saxonjs-poc/undefined')https://stackoverflow.com/questions/63187173
复制相似问题