Project

Profile

Help

Passing a value resulting from SaxonJS.XPath.evaluate as a parameter to fn:transform in further SaxonJS.XPath.evaluate as a typed parameter

Added by Martin Honnen over 1 year ago

Hi,

I need some help what could be wrong with my code. For the context, some users of my SaxonJS based XSLT 3 fiddle would like to have a separate dialog to provide any parameter values for global stylesheet parameters. And one idea is to allow to provide that value as an XPath expression to be evaluated.

The prototype I have written works as long as the parameter has no "as" declaring a type, but with a simple as="xs:string", the code fails with a seeminly contradicting error message Supplied value (string) foo does not match required type xs:string in stylesheet parameter Q{}p1. Or perhaps that error message tells where I have gone wrong and I need help to tell me how to understand why the type is wrong and how to fix the code to pass in an XPath evaluated value from JS to SaxonJS XPath/XSLT:

A quick demo code (written for SaxonJS under Node, tested with SaxonJS 2.5) is

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

const xslt1 = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">

  <xsl:param name="p1" as="xs:string"/>

  <xsl:template match="/" name="xsl:initial-template">
    <parameter-test>$p1 is {$p1}</parameter-test>
    <xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} {system-property('Q{http://saxon.sf.net/}platform')}</xsl:comment>
  </xsl:template>
  
</xsl:stylesheet>`;

const paramString1 = `"foo"`;

const xdmParamMap1 = new SaxonJS.XdmMap();

const qname = SaxonJS.XS.QName.fromEQName('Q{}p1');

const value = [SaxonJS.XPath.evaluate(paramString1)];

xdmParamMap1.inSituPut(qname, value);

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

console.log(result);

Complete error I get is

C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4578
null!==ha&&(oa=F.rb(ha,!0));R.collectionFinder=function(B){return"undefined"===typeof B?null:oa[B]};ha=!["raw","serialized"].includes(w);try{ja(S);"true"===S.relocatable&&(R.isRelocatableStylesheet=!0);if(R.stylesheetNode||R.packageText||R.packageNode)R.stylesheetInternal=S;var ba=A.checkOptions(R);A.internalTransform(S,da,ba);var ua=ba.principalResult}catch(B){if(B instanceof L&&B.Ze)throw new L(B.message,B.code.toString());throw B;}if(ua&&ha){ob.ca(Array.isArray(ua));da=Me(d);var Ga=Re.getComplexContentOutputter(da);



                                                                     ^
Error
    at new L (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4109:549)
    at w (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4604:117)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4637:287
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4650:124
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4625:263
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4623:472
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4632:430
    at Array.map (<anonymous>)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4632:407
    at Sa.g (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4588:500)
    at Va (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:9:299)
    at Wa.next (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:10:91)
    at zc.next (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4217:288)
    at string-join (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4558:331)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4632:444
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4389:408
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4358:33
    at Array.forEach (<anonymous>)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4358:6
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4380:97
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4358:33
    at Array.forEach (<anonymous>)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4358:6
    at Object.push (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4390:143)
    at Object.e [as internalTransform] (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4988:69)
    at transform (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4578:303)
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4632:444
    at C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4646:463
    at Object.I [as evaluate] (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4600:203)
    at Object.evaluate (C:\Users\marti\AppData\Roaming\npm\node_modules\saxon-js\SaxonJS2N.js:4936:362)
    at Object.<anonymous> (C:\Users\marti\OneDrive\Documents\xslt\blog-xslt-3-by-example\saxonjs-param-types\paramTest2.js:28:30)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  message: 'Supplied value (string) foo does not match required type xs:string in stylesheet parameter Q{}p1',
  name: 'XError',
  code: 'XPTY0004',
  xsltLineNr: '9',
  xsltModule: 'NoStylesheetBaseURI'
}

Replies (2)

RE: Passing a value resulting from SaxonJS.XPath.evaluate as a parameter to fn:transform in further SaxonJS.XPath.evaluate as a typed parameter - Added by Martin Honnen over 1 year ago

I think I have found a fix, using const value = SaxonJS.XPath.evaluate(paramString1, null, { resultForm: 'xdm' }); seems to fix it. Sigh, too many options on possible results to get it first on the first five attempts, but I guess that happens with a powerful and flexible API.

RE: Passing a value resulting from SaxonJS.XPath.evaluate as a parameter to fn:transform in further SaxonJS.XPath.evaluate as a typed parameter - Added by Michael Kay over 1 year ago

My guess would be that it's confusion between a Javascript string and an XDM string, but I'll leave the SaxonJS team to look at it more closely.

    (1-2/2)

    Please register to reply