inVoKer
inVoKer

Reputation: 59

SaxonJS - Prevent Server Reloading When Stylesheet Changes

I am using Saxon-JS to convert an XML file to an 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"

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.

Upvotes: 0

Views: 169

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167506

Using SaxonJS.XPath.evaluate('transform(...)', ..., { ... }) it is also possible to run XSLT directly. 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. 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.

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);

Upvotes: 1

Related Questions