Bug #4978
closed
Fix Committed on JS Branch:
2
Description
I'm trying to make the SEF compilation dynamic but I'm not sure I'm using transform()
right. Anyway here it is:
<script type="text/javascript">
<![CDATA[
window.onload = function() {
console.log(SaxonJS.getProcessorInfo().productName + " " +
SaxonJS.getProcessorInfo().productVersion + " " + SaxonJS.getProcessorInfo().releaseDate);
SaxonJS.XPath.evaluate('transform(map{ "stylesheet-location": "test-pi.xsl", "initial-template": "main" })');
}
]]>
</script>
Can be seen in https://namedgraph.github.io/saxon-js2-test/bootstrap2.xsl which is used as the `xml-stylesheet` processing instruction to bootstrap Saxon-JS.
Which produces an error:
SaxonJS2.rt.js:574 Uncaught TypeError: a.ha is not a function
at SaxonJS2.rt.js:574
at h (SaxonJS2.rt.js:568)
at transform (SaxonJS2.rt.js:574)
at SaxonJS2.rt.js:633
at Object.a [as evaluate] (SaxonJS2.rt.js:592)
at Object.evaluate (SaxonJS2.rt.js:790)
at window.onload (test-pi2.xml:7)
Can be seen in the console log of https://namedgraph.github.io/saxon-js2-test/test-pi2.xml
Oops I messed up the formatting...
- Assignee set to Norm Tovey-Walsh
(From a thread on the XML.com slack)
The error reported above is against the RT version which would never work. With the full JS file, alas, an error still occurs:
SaxonJS2.js:4391 Uncaught TypeError: T.$ is not a function
at SaxonJS2.js:4391
at A (SaxonJS2.js:4385)
at transform (SaxonJS2.js:4391)
at SaxonJS2.js:4456
at Object.U [as evaluate] (SaxonJS2.js:4425)
at Object.evaluate (SaxonJS2.js:4663)
at window.onload (test-pi2.xml:7)
- Status changed from New to In Progress
The error appears to be in the attempt to convert the initial-template
value into a QName. With the debug version of Saxon-JS, the error is that $a$$.toEQName
is not a function.
Martin Honnen has suggested to wrap the initial-template
name into a QName
, and the error went away:
SaxonJS.XPath.evaluate('transform(map{ "stylesheet-location": "test-pi.xsl", "initial-template": "QName('main')" })')
However the event handlers are not attached, so it doesn't look like this is a viable alternative to SaxonJS.transform()
. I tried this approach in an attempt to avoid having to pre-process the SEF file.
Sorry this is the correct snippet:
SaxonJS.XPath.evaluate('transform(map{ "stylesheet-location": "test-pi.xsl", "initial-template": QName("", "main") })');
The fn:transform
spec https://www.w3.org/TR/xpath-functions/#func-transform defines the initial-template
not as a string but as an xs:QName
:
initial-template 2.0, 3.0 The name of a named template in the stylesheet to act as the initial entry point.
Type: xs:QName
so I think while the error message is not helpful to understand that the attempt to simply use a string value main
instead of QName('', 'main')
fails it is probably not a bug in Saxon-JS.
One question that remains:
whether using fn:transform
from the JavaScript API and XPath.evaluate
allows you to inject event handlers or in general target the HTML document
I think the answer to that question is, "no." By passing 'document' as the context item to transform()
, it's possible to get the stylesheet to transform the source HTML, but ultimately what evaluate does is evaluate an expression and return the results. It isn't connected, interactively to the displayed HTML page.
- Status changed from In Progress to Resolved
The a.ha
message is ultimately caused by the fact that a string is being passed to the initial-template
instead of a QName. Saxon-JS was assuming it was a QName and attempting to call one of its methods (.toEQName
) which String
doesn't have.
I've fixed this by adding a type assertion function and calling it in this case (and a few others). The stylesheet still fails, but the error message is much more useful!
Just to add a word of explanation to any readers who might be mystified: We compile Saxon-JS using the Closure optimizing compiler, and one of the things it does is to rename everything. So the parameter has been renamed "a" and the method has been renamed "ha", so instead of a message like "function param.getEQName does not exist", we get "function a.ha does not exist".
Javascript, of course, being a weakly typed language, means that such errors are never reported until run-time, which makes them much harder to eliminate.
- Status changed from Resolved to Closed
This issue has been resolved in the Saxon 2.2 maintenance release.
- % Done changed from 0 to 100
- Fixed in JS Release set to Saxon-JS 2.2
- Fix Committed on JS Branch 2 added
Please register to edit this issue
Also available in: Atom
PDF
Tracking page