Project

Profile

Help

"Error parsing supplied SEF" when attempting transform using property stylesheetText

Added by charlie garrett-jones over 2 years ago

Hi there,

we are using the xslt3 tool to precompile xslt into SEF files for transformation in node. we were hoping that we could store the SEF contents as a string in the database but anytime we attempt to use a string with the SEF contents as the stylesheetText property then we get an "Error parsing supplied SEF" error.

if we take the same string and simply use it as a JSON object in node and use the stylesheetInternal transform property then everything works fine.

if we update the SEF checksum in the SEF string with the checksum reported in the error then it also works fine.

We've attempted to JSON.parse the SEF string value so that we can pass it subsequently as stylesheetInternal but it fails parsing.

Pulling the SEF data from a file resource also works fine.

additionally, we have noticed that the problem is introduced with the inclusion of loose text or the xsl:text attribute. so that the following works fine:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:eml="https://eml.ecoinformatics.org/eml-2.2.0"
    exclude-result-prefixes="xs"
    version="1.0">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="/"><xsl:value-of select="/eml:eml/dataset/title"/>
    <xsl:for-each select="//project|//relatedProject"><xsl:value-of select="./@id"/></xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

but this does not:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:eml="https://eml.ecoinformatics.org/eml-2.2.0"
    exclude-result-prefixes="xs"
    version="1.0">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="/"><xsl:text>"</xsl:text><xsl:value-of select="/eml:eml/dataset/title"/><xsl:text>"</xsl:text>
    </xsl:template>
</xsl:stylesheet>

hope this makes sense. any clues most appreciated.


Replies (7)

Please register to reply

RE: "Error parsing supplied SEF" when attempting transform using property stylesheetText - Added by Michael Kay over 2 years ago

Two questions:

(a) could you please give the exact error message? You seem to suggest that it mentioned checksums, but you haven't told us exactly what it said.

(b) how did you convert the file containing the generated SEF to a string, in order to supply it as the stylesheet-text property? Did you apply any escaping, and if so how? My guess would be that in converting the SEF to a string you inadverently changed something that affected the checksum.

RE: "Error parsing supplied SEF" when attempting transform using property stylesheetText - Added by charlie garrett-jones over 2 years ago

thanks for the quick response!

sorry but we are unable to determine how we managed to get the checksum error and correct it... will keep looking. the parsing error still persists.

so as a test case if our xslt is thus:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:eml="https://eml.ecoinformatics.org/eml-2.2.0"
    exclude-result-prefixes="xs"
    version="1.0">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="/">"datasetName": "<xsl:value-of select="/eml:eml/dataset/title"/>"
    </xsl:template>
</xsl:stylesheet>

we convert to SEF using xslt3 thus:

npx xslt3 -xsl:transforms/sims_eml_transform.2.xsl -export:transforms/sims_eml_transform.2.sef.json -t -ns:##html5

copy the output from the sef directly into a string in node:

var saxonjs = require('saxon-js');

var sef = '{"N":"package","version":"10","packageVersion":"1","saxonVersion":"SaxonJS 2.4","target":"JS","targetVersion":"2","name":"TOP-LEVEL","relocatable":"false","buildDateTime":"2022-05-31T14:06:34.639-07:00","ns":"xml=~ xsl=~ xs=~ eml=https://eml.ecoinformatics.org/eml-2.2.0","C":[{"N":"co","binds":"","id":"0","C":[{"N":"mode","onNo":"TC","flags":"","patternSlots":"0","prec":"","C":[{"N":"templateRule","rank":"0","prec":"0","seq":"0","ns":"xml=~ xsl=~ xs=~ eml=https://eml.ecoinformatics.org/eml-2.2.0","minImp":"0","flags":"s","slots":"200","baseUri":"file:///home/ubuntu64/node/saxonjs/transforms/sims_eml_transform.2.xsl","line":"8","module":"sims_eml_transform.2.xsl","expand-text":"false","match":"/","prio":"-0.5","matches":"ND","C":[{"N":"p.nodeTest","role":"match","test":"ND","sType":"1ND","ns":"= xml=~ fn=~ xsl=~ xs=~ eml=https://eml.ecoinformatics.org/eml-2.2.0 "},{"N":"sequence","role":"action","sType":"*NT ","C":[{"N":"valueOf","sType":"1NT ","C":[{"N":"str","sType":"1AS ","val":"\"datasetName\": \""}]},{"N":"valueOf","flags":"l","sType":"1NT ","C":[{"N":"fn","name":"string-join","role":"select","C":[{"N":"first","C":[{"N":"forEach","sType":"*AS ","C":[{"N":"data","sType":"*A ","C":[{"N":"mergeAdj","C":[{"N":"docOrder","sType":"*NE","role":"select","line":"2","C":[{"N":"docOrder","sType":"*NE","ns":"= xml=~ fn=~ xsl=~ xs=~ eml=https://eml.ecoinformatics.org/eml-2.2.0 ","C":[{"N":"slash","op":"/","C":[{"N":"slash","op":"/","C":[{"N":"slash","op":"/","C":[{"N":"root"},{"N":"axis","name":"child","nodeTest":"*NE nQ{https://eml.ecoinformatics.org/eml-2.2.0}eml"}]},{"N":"axis","name":"child","nodeTest":"*NE u[NE nQ{}dataset,NE nQ{http://www.w3.org/1999/xhtml}dataset]"}]},{"N":"axis","name":"child","nodeTest":"*NE u[NE nQ{}title,NE nQ{http://www.w3.org/1999/xhtml}title]"}]}]}]}]}]},{"N":"fn","name":"string","sType":"1AS ","C":[{"N":"dot"}]}]}]},{"N":"str","sType":"1AS ","val":" "}]}]},{"N":"valueOf","sType":"1NT ","C":[{"N":"str","sType":"1AS ","val":"\"\n    "}]}]}]}]}]},{"N":"overridden"},{"N":"output","C":[{"N":"property","name":"Q{http://saxon.sf.net/}stylesheet-version","value":"10"},{"N":"property","name":"omit-xml-declaration","value":"yes"}]},{"N":"decimalFormat"}],"Σ":"b434ef2d"}';

saxonjs.transform({
  stylesheetText: sef,
  sourceFileName: "transforms/generated_eml.xml",
  destination: "serialized",
  logLevel: 10
}, "async")
.then (output => {
  console.log(output.principalResult);
});

the error:

ubuntu64@DeepThought:~/node/saxonjs$ node test.js

/home/ubuntu64/node/saxonjs/node_modules/saxon-js/SaxonJS2N.js:4840 A;ob.fb("Transform with options: "+G(z),2);try{var B=r(z);B.Ca&&(B.outputProperties=k(B.Ca))}catch(O){if(A)throw O;return Promise.reject(O)}var R=Z.baseURL();if(B.Jb)var J=B.Jb=F(B.Jb);else B.fc&&(J=B.fc=F(B.fc));B.Vf=J||B.Vf;if(B.Wc)var fa=B.Wc=F(B.Wc);else B.Vc&&(fa=B.Vc=F(B.Vc));B.Zd=fa||B.Zd;"function"===typeof Z.deliverMessage&&Z.deliverMessage(z);"number"===typeof B.If&&ob.setLogLevel(B.If);if(A)return t(B);B.Xh=!0;z=[];var ka=B.stylesheetInternal||null;if(!ka)if(J=B.stylesheetText)try{ka=JSON.parse(J)}catch(O){throw new L("Error parsing supplied SEF", ^ Error at new L (/home/ubuntu64/node/saxonjs/node_modules/saxon-js/SaxonJS2N.js:3944:463) at Object.transform (/home/ubuntu64/node/saxonjs/node_modules/saxon-js/SaxonJS2N.js:4840:530) at Object. (/home/ubuntu64/node/saxonjs/test.js:8:9) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 { message: 'Error parsing supplied SEF', name: 'XError', code: 'SXJS0006' }

hope this provides sufficient detail.

thanks again!

RE: "Error parsing supplied SEF" when attempting transform using property stylesheetText - Added by charlie garrett-jones over 2 years ago

but this works fine (object instead of string):

var saxonjs = require('saxon-js');

var sef = {"N":"package","version":"10","packageVersion":"1","saxonVersion":"SaxonJS 2.4","target":"JS","targetVersion":"2","name":"TOP-LEVEL","relocatable":"false","buildDateTime":"2022-05-31T14:06:34.639-07:00","ns":"xml=~ xsl=~ xs=~ eml=https://eml.ecoinformatics.org/eml-2.2.0","C":[{"N":"co","binds":"","id":"0","C":[{"N":"mode","onNo":"TC","flags":"","patternSlots":"0","prec":"","C":[{"N":"templateRule","rank":"0","prec":"0","seq":"0","ns":"xml=~ xsl=~ xs=~ eml=https://eml.ecoinformatics.org/eml-2.2.0","minImp":"0","flags":"s","slots":"200","baseUri":"file:///home/ubuntu64/node/saxonjs/transforms/sims_eml_transform.2.xsl","line":"8","module":"sims_eml_transform.2.xsl","expand-text":"false","match":"/","prio":"-0.5","matches":"ND","C":[{"N":"p.nodeTest","role":"match","test":"ND","sType":"1ND","ns":"= xml=~ fn=~ xsl=~ xs=~ eml=https://eml.ecoinformatics.org/eml-2.2.0 "},{"N":"sequence","role":"action","sType":"*NT ","C":[{"N":"valueOf","sType":"1NT ","C":[{"N":"str","sType":"1AS ","val":"\"datasetName\": \""}]},{"N":"valueOf","flags":"l","sType":"1NT ","C":[{"N":"fn","name":"string-join","role":"select","C":[{"N":"first","C":[{"N":"forEach","sType":"*AS ","C":[{"N":"data","sType":"*A ","C":[{"N":"mergeAdj","C":[{"N":"docOrder","sType":"*NE","role":"select","line":"2","C":[{"N":"docOrder","sType":"*NE","ns":"= xml=~ fn=~ xsl=~ xs=~ eml=https://eml.ecoinformatics.org/eml-2.2.0 ","C":[{"N":"slash","op":"/","C":[{"N":"slash","op":"/","C":[{"N":"slash","op":"/","C":[{"N":"root"},{"N":"axis","name":"child","nodeTest":"*NE nQ{https://eml.ecoinformatics.org/eml-2.2.0}eml"}]},{"N":"axis","name":"child","nodeTest":"*NE u[NE nQ{}dataset,NE nQ{http://www.w3.org/1999/xhtml}dataset]"}]},{"N":"axis","name":"child","nodeTest":"*NE u[NE nQ{}title,NE nQ{http://www.w3.org/1999/xhtml}title]"}]}]}]}]}]},{"N":"fn","name":"string","sType":"1AS ","C":[{"N":"dot"}]}]}]},{"N":"str","sType":"1AS ","val":" "}]}]},{"N":"valueOf","sType":"1NT ","C":[{"N":"str","sType":"1AS ","val":"\"\n    "}]}]}]}]}]},{"N":"overridden"},{"N":"output","C":[{"N":"property","name":"Q{http://saxon.sf.net/}stylesheet-version","value":"10"},{"N":"property","name":"omit-xml-declaration","value":"yes"}]},{"N":"decimalFormat"}],"Σ":"b434ef2d"};

saxonjs.transform({
  stylesheetInternal: sef,
  sourceFileName: "transforms/generated_eml.xml",
  destination: "serialized",
  logLevel: 10
}, "async")
.then (output => {
  console.log(output.principalResult);
});

RE: "Error parsing supplied SEF" when attempting transform using property stylesheetText - Added by Martin Honnen over 2 years ago

I think you need to make sure you read in the file from the file system with e.g.

const SaxonJS = require('saxon-js');
const fs = require('fs');

const sef = fs.readFileSync('sheet.sef.json', 'utf8');

SaxonJS.transform({
  stylesheetText: sef,
  sourceFileName: "sample1.xml",
  destination: "serialized",
  logLevel: 10
}, "async")
.then (output => {
  console.log(output.principalResult);
});

If you copy/paste the JSON from an editor into a JavaScript string literal delimited by quotes you basically have, in the case of the code containing an escaped \", some string that lacks the proper escaping, you would probably need to double backslashes or at least do some doube escaping due to the use of JSON inside of a JavaScript string literal.

RE: "Error parsing supplied SEF" when attempting transform using property stylesheetText - Added by Debbie Lockett over 2 years ago

As Martin has commented, the problem is the escaping of \" (in particular at "val":"\"datasetName\": \"" in your SEF). So simply copying the SEF JSON and pasting into a string will not work.

As you've noted, you can instead use stylesheetInternal with the SEF JSON directly. Alternatively, to use stylesheetText you would need to call JSON.stringify() on the SEF JSON object to produce a suitable string with the right escaping.

Otherwise, indeed reading the SEF from a file resource (e.g. using fs.readFileSync) produces a string with the correct escaping that can be supplied to stylesheetText.

(Note that internally, SaxonJS is doing JSON.parse() on the string supplied for stylesheetText.)

RE: "Error parsing supplied SEF" when attempting transform using property stylesheetText - Added by Michael Kay over 2 years ago

Just in case you're going through the same hoops as I was to understand this: there's a text node in the stylesheet whose value is

"datasetName" : "

and to put this in JSON it has to be escaped as

"val":"\"datasetName\": \""

and to put that JSON into a Javascript string, all the quotes and backslashes have to be escaped again so it becomes

\"val\":\"\\\"datasetName\\\": \\\"\"

The alternative would be to use a "raw string" with backtick notation.

RE: "Error parsing supplied SEF" when attempting transform using property stylesheetText - Added by charlie garrett-jones over 2 years ago

thanks so much for all of these great responses!

yes, thats a bunch of escaping!! i believe we've decided to change our approach and access the SEF data via file system or URI and avoid some of that complexity.

thanks again!

    (1-7/7)

    Please register to reply