简体   繁体   English

热访问转换的主体结果? [撒克逊JS]

[英]Hot to access principalResult of the transformation? [saxonJS]

I migrate from saxon-CE to saxonJS (v1.2.0)我从 saxon-CE 迁移到 saxonJS (v1.2.0)

The output of the XSLT transformation need to be captured as an XML Document object as it was in saxon-CE: XSLT 转换的 output 需要捕获为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 的内容:

It tried to apply SaxonJS this way:它试图以这种方式应用 SaxonJS:

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

and expected to get a transformation results object where I can access the principalResult property as described in the official documentation (#destination) and in this presentation .并期望获得转换结果 object ,我可以在其中访问principalResult属性,如官方文档(#destination) 和本演示文稿中所述。

When running the code I obtain the following:运行代码时,我获得以下信息:

在此处输入图像描述

There is no problem with transformation itself: when destination is set to replaceBody it works as expected.转换本身没有问题:当destination设置为replaceBody时,它会按预期工作。

Eventually I solved my task with the following code (Saxon JS 2.3):最终我用下面的代码(Saxon JS 2.3)解决了我的任务:

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

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

The SEF could be produced by xslt3 tool . SEF 可以由xslt3 工具生成。 Note that you might use the alternative command for this (windows 10 power shell):请注意,您可以为此使用替代命令(windows 10 power shell):

node node_modules/xslt3/xslt3.js "-t" "-xsl:stylesheet.xsl" "-export:stylesheet.sef.json" "-nogo"

One way is, of course, to use the fn:transform function with "normal" XSLT code:当然,一种方法是将fn:transform function 与“正常”XSLT 代码一起使用:

 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.出于性能原因不推荐,但可能是避免创建 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 .如果你想创建一个 SEF,请注意,来自 Saxonica 的 Node.js xslt3工具也可以做到这一点,你不需要当前版本的 Saxon EE,只xslt3 -t -export:my-sheet.sef.json -nogo -xsl:my-sheet.xsl

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

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