Project

Profile

Help

resultDocuments property of transform call result not populated?

Added by Martin Honnen over 2 years ago

I have revisited the population of the resultDocuments property of the result of the transform Saxon-JS API function with 2.4 under Node.js but I still don't see it populated.

According to the documentation https://www.saxonica.com/saxon-js/documentation2/index.html#!api/transform, I would expect it to be populated in the result object from a transform call if destination : 'serialized' is set as the option and no deliverResultDocument function is set up or if that function is set up but returns an object without a save function property. I find no resultDocuments property populated if I provide no deliverResultDocument function and if I provide one but don't set a save function I continue to run into the issue mentioned in https://saxonica.plan.io/boards/5/topics/7940, i.e. an error.

So the code

const path = require('path');

var urlModule = require('url');

const baseURI = urlModule.pathToFileURL(path.join(path.resolve('.'), 'async-result1.xml'));

console.log(`baseURI: ${baseURI.href}`);

var destination = 'serialized'; //'file';

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

var options = { 
  stylesheetLocation : 'sheet1.sef.json',
  sourceLocation: 'input1.xml',
  destination: destination,
  //deliverResultDocument: function(uri) {
  //  console.log(`Processing result uri ${uri}`); 
  //  return { destination: destination }; 
  //},
  baseOutputURI: baseURI.href
};

SaxonJS.transform(options, 'async').then(result => {
  console.log(`principalResult: ${result.principalResult}`);

  for (var uri in result.resultDocuments) {
    console.log(`result URI ${uri}: ${options.resultDocuments[uri]}`);
  }
}).catch(err => console.log(err));

when run against an input of e.g.

<items>
  <item>a</item>
  <item>b</item>
  <item>c</item>
</items>

with the stylesheet being compiled with Saxon-JS 2.4 from the XSLT code

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:saxon="http://saxon.sf.net/"
  exclude-result-prefixes="#all"
  expand-text="yes">

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

  <xsl:template match="item">
    <xsl:next-match/>
    <xsl:variable name="pos" as="xs:integer">
      <xsl:number/>
    </xsl:variable>
    <xsl:result-document href="item-{$pos}.xml">
      <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')} at {saxon:timestamp()}</xsl:comment>
    </xsl:result-document>
  </xsl:template>

  <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')} at {saxon:timestamp()}</xsl:comment>
  </xsl:template>

</xsl:stylesheet>

gives an output like

baseURI: file:///C:/Users/SomeUser/SomeFolder/async-result1.xml
principalResult: <?xml version="1.0" encoding="UTF-8"?>
<items>
   <item>a</item>
   <item>b</item>
   <item>c</item>
</items>
<!--Run with SaxonJS 2.4 Node.js at 2022-05-13T17:42:01.971+02:00-->

meaning the result has captured the principalResult, but it doesn't have any resultDocuments property captured and populated as part of the result. Instead, if I look into the working directory, three result files item-1.xml, item-2.xml and item-3.xml have been created (or been overwritten).

If I uncomment the deliverResultDocument function having no save property, as in

const path = require('path');

var urlModule = require('url');

const baseURI = urlModule.pathToFileURL(path.join(path.resolve('.'), 'async-result1.xml'));

console.log(`baseURI: ${baseURI.href}`);

var destination = 'serialized'; //'file';

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

var options = { 
  stylesheetLocation : 'sheet1.sef.json',
  sourceLocation: 'input1.xml',
  destination: destination,
  deliverResultDocument: function(uri) {
    console.log(`Processing result uri ${uri}`); 
    return { destination: destination }; 
  },
  baseOutputURI: baseURI.href
};

SaxonJS.transform(options, 'async').then(result => {
  console.log(`principalResult: ${result.principalResult}`);

  for (var uri in result.resultDocuments) {
    console.log(`result URI ${uri}: ${options.resultDocuments[uri]}`);
  }
}).catch(err => console.log(err));

I get the linked error of save not being a function:

baseURI: file:///C:/Users/SomeUser/SomeFolder/async-result1.xml
Processing result uri file:///C:/Users/SomeUser/SomeFolder/item-1.xml
Transformation failure: TypeError: Af.save is not a function
TypeError: Af.save is not a function
    at W (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4492:193)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4496:498
    at M (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4191:641)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4220:309
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4192:33
    at Array.forEach (<anonymous>)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4192:6
    at Object.push (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4224:130)
    at e.hf (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4805:4)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4802:284
    at wc.ba.forEachItem (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4041:583)
    at e.Db (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4802:251)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4800:414
    at Object.r [as mh] (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4195:440)
    at SC (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4800:310)
    at e.hf (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4805:85)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4802:284
    at wc.ba.forEachItem (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4041:583)
    at e.Db (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4802:251)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4800:414
    at Object.r [as mh] (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4195:294)
    at SC (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4800:310)
    at e.hf (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4805:85)
    at e.Gk (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4803:70)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4221:428
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4192:33
    at Array.forEach (<anonymous>)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4192:6
    at Object.push (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4224:130)
    at e.hf (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4805:4)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4802:284
    at wc.ba.forEachItem (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4041:583)
    at e.Db (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4802:251)
    at e (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4817:280)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4843:339

Is there any working sample of having resultDocuments populated by Saxon-JS in the transform() result?


Replies (1)

RE: resultDocuments property of transform call result not populated? - Added by Debbie Lockett over 2 years ago

Thanks for raising this. I've now opened a bug to look into the problems, please see Bug #5493: Problems with transform result output.resultDocuments

When deliverResultDocument is not supplied at all, actually I do not expect resultDocuments to be populated since the default for deliverResultDocument on Node.js says destination: "file", see the documentation at https://www.saxonica.com/saxon-js/documentation2/index.html#!api/transform:

In the absence of the deliverResultDocument option, SaxonJS effectively uses a default in which: destination is (...) "file" in Node.js

Note that I don't think the options.destination property is relevant for secondary results.

However, you're right that there is a bug when supplying the deliverResultDocument function having no save property but destination: "serialized". And we are lacking examples (and tests).

    (1-1/1)

    Please register to reply