Feature #5057
closed
Applies to JS Branch:
2, Trunk
Fix Committed on JS Branch:
Trunk
Description
Hi. I cannot seem to find a native way to construct JS objects, which makes porting JS code to XSLT complicated. It think it was possible with Saxon-CE, but I might be wrong.
I know there is ixsl:eval()
, but since it accepts string as parameter, how does one pass a JS object from a $var
for example? It does not seem possible.
So how would I port some simple JS code like the following?
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([37.41, 8.82]),
zoom: 4
})
});
https://openlayers.org/en/latest/doc/quickstart.html
As always Martin Honnen has a solution:
<xsl:function name="mf:construct" as="item()">
<xsl:param name="constructor" as="xs:string"/>
<xsl:param name="arguments" as="array(*)"/>
<xsl:sequence
select="ixsl:window()
=> ixsl:call(
'Reflect.construct',
[ixsl:window() => ixsl:get($constructor), $arguments]
)"/>
</xsl:function>
I just think it feels like that should be a built-in function under the ixsl
namespace.
Hi Martynas,
I'm having a little trouble understanding exactly what you're trying to do. I think you're probably right that an ixsl
function makes sense, but I'd like to be able to construct some more test cases.
In the initial report where you've got the var map = ...
example. Are you saying you'd like to be able to construct that whole map as a JavaScript object from the Saxon-JS side?
It seems to me you could construct it as an XDM map pretty easily. Is there a context where you want to use it that doesn't work if it's an XDM map?
Sorry if I'm being slow on the uptake.
Basically I'm looking for a way to invoke the new
keyword. Yes I'm looking to construct the object from the Saxon-JS side, or rather multiple objects I could store in separate variables and then compose as necessary. I'd like to be able to port JS code like that to Saxon-JS.
Maybe it can be solved using XDM maps -- could I please see a small example of that? I was looking at the XDM to JavaScript Conversion and my impression was that there is not a direct way to construct a JS object as an XDM map? How do you specify the class name such as ol.Map
or ol.source.OSM
?
Right. I was fixated on the "object" part not the "new" part. I think the workaround that Martynas proposed might be the current workaround. Another possibility, if there are only a small number of objects like this, would be to create global JavaScript functions that do the construction.
Yes the workaround works. I would like to propose an ixsl:new
extension function though. For example: ixsl:new('Class', [ $arg ])
.
- Related to Feature #5530: For Saxon-JS under Node, expose global as ixsl:window() or ixsl:global() added
- Priority changed from Normal to High
- Sprint/Milestone set to SaxonJS 3.0
ixsl:new()
without arguments could be used to create an empty JSON object ({ }
).
- Category set to IXSL extensions
- Status changed from New to Resolved
- Applies to JS Branch Trunk added
- Fix Committed on JS Branch Trunk added
ixsl:new()
will be available in SaxonJS 3.
Please register to edit this issue
Also available in: Atom
PDF
Tracking page