Project

Profile

Help

Support #6281

closed

Issue with Saxon2JS v2.6

Added by Robert Kirkpatrick 12 months ago. Updated 11 months ago.

Status:
Closed
Priority:
Normal
Category:
-
Sprint/Milestone:
-
Start date:
2023-12-05
Due date:
% Done:

100%

Estimated time:
Applies to JS Branch:
2
Fix Committed on JS Branch:
Fixed in JS Release:
SEF Generated with:
Platforms:
Company:
-
Contact person:
-
Additional contact persons:
-

Description

SaxonJS2.rt.js:781 Uncaught (in promise) TypeError: H.replace is not a function
    at Object.parseXmlFromString (SaxonJS2.rt.js:781:182)
    at p (SaxonJS2.rt.js:1093:75)
    at Object.transform (SaxonJS2.rt.js:1112:97)
    at doc.html:12:22
parseXmlFromString @ SaxonJS2.rt.js:781
p @ SaxonJS2.rt.js:1093
transform @ SaxonJS2.rt.js:1112
(anonymous) @ doc.html:12
Promise.then (async)
transformText @ doc.html:17
(anonymous) @ doc.html:6
Promise.then (async)
load @ doc.html:6
window.onload @ doc.html:26
load (async)
(anonymous) @ doc.html:26

Files

report.zip (2.05 KB) report.zip sources Robert Kirkpatrick, 2023-12-05 17:14
Actions #1

Updated by Martin Honnen 12 months ago

Let's hope someone sees this thread and moves it to the SaxonJS support site https://saxonica.plan.io/projects/saxon-js.

Anyway, I don't have a fix for the code you have used but I wonder whether you actually need to do all the fetch calls, a straightforward, direct use of simply transform seems to work without problems:

<html><head><script src="SaxonJS2.rt.js"></script>
<script>
function transformText(doc, sef, param) {
	 SaxonJS.transform( 
			{ sourceLocation: doc, 
                          stylesheetLocation: sef,
			  destination: 'serialized'
                        }
			, 'async')
		.then(r => r.principalResult)
                .then(doc => replace(doc));
	}

function replace(doc) {
	document.open("text/html", "replace");
	document.write(doc);
	document.close();
	}
window.onerror= (er) => { alert(er) }
window.onload= () => { transformText('doc.xml', 'doc.sef.json', 1) }
</script></head>
<body></body></html>

Perhaps that helps as a workaround.

If you do set stylesheetText, then I also think you are supposed to set stylesheetBaseURI, though this doesn't seem to prevent the error just shift it to a different position.

Actions #2

Updated by Robert Kirkpatrick 12 months ago

Tx Martin for your quick reply and sorry for reporting to the wrong thread.

This case is a simplistic version of a real-life project where I have selected the Fetch API to circumvent CORS restrictions, which led me to use the Text options of SaxonJS.transform. If this is the cause of the failure, I would at least expect a clearer error message.

Kr, Robert.

Actions #3

Updated by Michael Kay 12 months ago

  • Project changed from Saxon to SaxonJS
  • Category deleted (Saxon extensions)
Actions #4

Updated by Debbie Lockett 12 months ago

  • Tracker changed from Bug to Support
  • Description updated (diff)
  • Status changed from New to In Progress
  • Assignee set to Debbie Lockett
  • Applies to JS Branch 2 added

So your JavaScript code is attempting to use fetch to preload the XML source and SEF, and these are then supplied to SaxonJS.transform() as strings using the stylesheetText and sourceText options. This should indeed work.

The problem is that you have not actually supplied a string for the sourceText option. You need:

function load(src, sef, param) {
	fetch(src)
		.then(f => f.text())
		.then(t => transformText(t, sef, param))
		.then(doc => replace(doc))
	}

rather than

function load(src, sef, param) {
	fetch(src)
		.then(t => transformText(t.text(), sef, param))
		.then(doc => replace(doc))
	}

otherwise you are passing a promise rather than a string into the call to transformText.

Furthermore, in your transformText function, you will need to actually return the promise; otherwise you do not get the page update (from the replace(doc) call) as intended. i.e.:

function transformText(doc, sef, param) {
	return fetch(sef, {mode: 'cors'})
		....

With these changes, the code now works for me. (And so I have changed this issue from "Bug" to "Support").

It is unfortunate that the error message is not clearer, and we could perhaps improve that (by checking that the value supplied for sourceText is a string). However this debugging was possible simply using console.log().

Hope this helps you move forward!

Actions #5

Updated by Robert Kirkpatrick 12 months ago

Thank you very much!

Actions #6

Updated by Debbie Lockett 11 months ago

  • Status changed from In Progress to Closed
  • % Done changed from 0 to 100

Please register to edit this issue

Also available in: Atom PDF Tracking page