简体   繁体   English

SaxonJS - 样式表更改时防止服务器重新加载

[英]SaxonJS - Prevent Server Reloading When Stylesheet Changes

I am using Saxon-JS to convert an XML file to an XSL-fo.我正在使用 Saxon-JS 将 XML 文件转换为 XSL-fo。 I follow the documentation and leverage the xslt3 command to first compile an XSL file to a sef file such as: "compile": "xslt3 -xsl:./src/styles/sample.xsl -export:sef/sample.sef.json -t -nogo -ns:##html5"我按照文档并利用 xslt3 命令首先将 XSL 文件编译为 sef 文件,例如: "compile": "xslt3 -xsl:./src/styles/sample.xsl -export:sef/sample.sef.json -t -nogo -ns:##html5"

However, this causes an issue that the server needs to reload and compile the stylesheet whenever there is a change in stylesheet.但是,这会导致服务器需要在样式表发生更改时重新加载和编译样式表的问题。

My question is: how to prevent the server from reloading when a change is made?我的问题是:如何防止服务器在进行更改时重新加载? Thank you in advance.先感谢您。

Using SaxonJS.XPath.evaluate('transform(...)', ..., {... }) it is also possible to run XSLT directly.使用SaxonJS.XPath.evaluate('transform(...)', ..., {... })也可以直接运行 XSLT。 Whether that works and performs for you is something you would need to test, I don't know how complex your XSLT is, whether it has includes or imports, how large the input XML and how large the resulting XSL-FO is.这是否对您有效和执行是您需要测试的事情,我不知道您的 XSLT 有多复杂,它是否包含或导入,输入 XML 有多大以及生成的 XSL-FO 有多大。 But if the target format is XSL-FO and not HTML in the browser (although that way I don't understand the use of -ns:##html5 ) you might not need the interactive Saxon extensions that the xslt3 compilation supports and adds.但是,如果浏览器中的目标格式是 XSL-FO 而不是 HTML(尽管那样我不明白-ns:##html5的用法),您可能不需要xslt3编译支持和添加的交互式 Saxon 扩展。

Simple example:简单示例:

const SaxonJS = require("saxon-js")

const xml = `<root>
  <item>a</item>
  <item>b</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"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  exclude-result-prefixes="xs"
  expand-text="yes">

  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="/" name="xsl:initial-template">
    <fo:root>
        <fo:layout-master-set>
            <fo:simple-page-master master-name="sample">
                <fo:region-body/>
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="sample">
            <fo:flow flow-name="xsl-region-body">
                <fo:block>
                  <xsl:apply-templates/>
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>  
  </xsl:template>
  
  <xsl:template match="root">
    <fo:list-block>
      <xsl:apply-templates/>
    </fo:list-block>
  </xsl:template>
  
  <xsl:template match="item">
    <fo:list-item>
      <fo:list-item-label end-indent="label-end()">
        <fo:block>
          <xsl:number format="1."/>
        </fo:block>
      </fo:list-item-label>
      <fo:list-item-body start-indent="body-start()">
        <fo:block>
          <xsl:apply-templates/>
        </fo:block>
      </fo:list-item-body>
    </fo:list-item>
  </xsl:template>
  
</xsl:stylesheet>`;

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

console.log(result);

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

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