0

我从 saxon-CE 迁移到 saxonJS (v1.2.0)

XSLT 转换的输出需要像XML Document object在 saxon-CE 中一样被捕获:

var xslPath = './*.xsl';
var xsl = Saxon.requestXML(xslPath);
var proc = Saxon.newXSLT20Processor(xsl);
var xmlDoc;
var xmlDocTransformed;
var xmlStr;

xmlDoc = Saxon.parseXML(app.getLoadedMEI());
xmlDocTransformed = proc.transformToDocument(xmlDoc);

变量 xmlDocTransformed 的内容:

它试图以这种方式应用 SaxonJS:

var result;
result = SaxonJS.transform({
         stylesheetLocation: "./*.sef.xml",
         sourceLocation: "./*.xml",
         destination: "application"
      });

并期望获得一个转换结果对象,我可以在其中访问官方文档(#destination) 和本演示文稿principalResult中描述的属性。

运行代码时,我获得以下信息:

在此处输入图像描述

转换本身没有问题:whendestination设置为replaceBody它按预期工作。

4

2 回答 2

1

最终我用以下代码(Saxon JS 2.3)解决了我的任务:

var options = {
        stylesheetLocation: xslPath,
        sourceText: inputXmlStr,
        stylesheetParams: params,
        destination: "document"
    };

    var result = SaxonJS.transform(options);
    transformedXmlStr = SaxonJS.serialize(result.principalResult);

SEF 可以通过xslt3 tool生成。请注意,您可以为此使用替代命令(windows 10 power shell):

node node_modules/xslt3/xslt3.js "-t" "-xsl:stylesheet.xsl" "-export:stylesheet.sef.json" "-nogo"
于 2022-03-04T13:25:44.020 回答
0

One way is, of course, to use the fn:transform function with "normal" XSLT code:

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

const xslt = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">

  <xsl:output method="xml"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="/" name="xsl:initial-template">
    <xsl:next-match/>
    <xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} {system-property('Q{http://saxon.sf.net/}platform')}</xsl:comment>
  </xsl:template>

</xsl:stylesheet>`;

const resultString = SaxonJS.XPath.evaluate(`transform(map { 'source-node' : parse-xml($xml), 'stylesheet-text' : $xslt, 'delivery-format' : 'serialized' })?output`, null, { params : { 'xml' : xml, 'xslt' : xslt } });

console.log(resultString);
<script src="https://martin-honnen.github.io/Saxon-JS-2.3/SaxonJS2.js"></script>

Not recommended for performance reason but perhaps a workaround that avoid the hassle of creating an SEF. If you want to create an SEF, note, that the Node.js xslt3 tool from Saxonica can also do that, you don't need a current version of Saxon EE, just xslt3 -t -export:my-sheet.sef.json -nogo -xsl:my-sheet.xsl.

于 2022-03-03T15:23:15.537 回答