Possible to serialize stylesheetInternal?
Added by Richard Tweeddale about 3 years ago
I'm writing an XSLT service using SaxonJS, where one stylesheet may be used many times, across many different requests.
Looking at the documentation, it looks it would be most efficient to re-use the stylesheetInternal
, that is returned as part of the result of SaxonJS.transform
.
I was wondering if it is possible to serialize this stylesheetInternal
so I can store it in a cache for later re-use? Or is it necessary to keep it in memory only?
I've tried both SaxonJS.serialize
and JSON.stringify
but neither of those work.
SaxonJS.serialize
just returns <?xml version="1.0" encoding="UTF-8"?>
and JSON.stringify
complains about circular references.
Here's my code:
import fs from 'fs';
import { execSync } from 'child_process';
import SaxonJS from 'saxon-js';
const defaultXMLPath = 'input.html'
const defaultStylesheetPath = 'stylesheet2.xslt'
const defaultSEFPath = 'stylesheet.sef.json'
execSync(`xslt3 -t -xsl:${defaultStylesheetPath} -export:${defaultSEFPath} -nogo`)
let xmlInput = await SaxonJS.getResource({file: defaultXMLPath})
let transformResult = SaxonJS.transform({stylesheetFileName: defaultSEFPath, sourceText: xmlInput, destination: "raw"})
let xsltOutput = transformResult.principalResult.data
let primedSef = transformResult.stylesheetInternal
console.log(`xsltOutput: ${xsltOutput}`)
// Check that primedSef works
let transformResult2 = SaxonJS.transform({stylesheetInternal: primedSef, sourceText: xmlInput, destination: "raw"})
let xsltOutput2 = transformResult2.principalResult.data
let primedSef2 = transformResult2.stylesheetInternal
console.log(`xsltOutput2: ${xsltOutput2}`)
// Serialize primedSef?
let serializedSef = SaxonJS.serialize(primedSef)
console.log(`serializedSef: ${serializedSef}`)
And the output:
Saxon-JS 2.1 from Saxonica
Node.js version v14.16.1
Compiling stylesheet /xslt-service/stylesheet2.xslt
Stylesheet compilation time: 0.075s
Stylesheet exported to SEF file /xslt-service/stylesheet.sef.json
xsltOutput:
xslt-success
xsltOutput2:
xslt-success
serializedSef: <?xml version="1.0" encoding="UTF-8"?>
Replies (2)
RE: Possible to serialize stylesheetInternal? - Added by Richard Tweeddale about 3 years ago
The flatted package (https://github.com/WebReflection/flatted) seems to work, and gives the right result for the very very basic example I'm using. Hopefully I can trust it for more complicated XSLT and XML files.
RE: Possible to serialize stylesheetInternal? - Added by Michael Kay about 3 years ago
Thanks for that information: I was going to say no! The internal form of the stylesheet is a data structure that makes very heavy use of Javascript anonymous functions and closures, so I am pleasantly surprised that flatted
can handle it.
Please register to reply