【问题标题】:How to convert xsl file to sef file如何将xsl文件转换为sef文件
【发布时间】:2020-11-21 00:36:30
【问题描述】:

我有xsl 文件,我想将其应用于node.js 中的一些XML 内容。我找到了saxon-js,它似乎提供了我需要的东西。虽然我在文档中有点迷失,但似乎我应该先将我的 xsl 文件转换为 .sef 文件,然后再在节点中运行转换。

saxon-js 自述文件提到了其他包,xslt3 应该能够做到这一点(see readme):

编译样式表

要将books.xsl 中保存的样式表编译为books.sef.json 中的SEF 文件,以便在浏览器或Node.js 中运行,请使用命令行:

xslt3 -xsl:books.xsl -export:books.sef.json -t -ns:##html5

但是,在安装xslt3 包后,没有可用于运行转换的可执行文件。

我错过了什么吗?如何将xsl 转换为sef.json(一次就可以,不需要从服务器动态运行它或其他任何东西)?

【问题讨论】:

  • 在我 yarn add xslt3 到一个项目之后,我得到了一个 ./node_modules/.bin/xslt3 文件/命令。
  • 你可以试试node node_modules/xslt3.js

标签: node.js saxon-js


【解决方案1】:

在将包安装为项目中的本地依赖项后,您可以在 ./node_modules/xslt3/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')

或者你可以在 npm 中使用 -g 标志来全局安装 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')

【讨论】:

    【解决方案2】:

    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 或其他。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 2017-01-05
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多