Re: [Saxon-JS 2] Creating a JSON object

Michael Kay <mike-JkSD5nQpfvpWk0Htik3J/[email protected]>
Newsgroups gmane.text.xml.saxon.help
Message-ID <[email protected]>
If you're doing this repeatedly, then you could consider writing a Javascript function that converts an XDM map to a JS object, and calling this function.

It would then look something like:

<xsl:variable name="map" as="map(*)">
  <xsl:map>
    <xsl:map-entry key="'subject'" select="'SPARQLBuilder.SelectBuilder.var(''child'')'"/>
    <xsl:map-entry key="'predicate'" select="'SPARQLBuilder.SelectBuilder.var(''' || $predicate || ''''"/>,
    <xsl:map-entry key="'object'" select="'SPARQLBuilder.SelectBuilder.var(''whatever'')'"/>
 </xsl:map>
</xsl:variable>
<xsl:variable name="bgp" select="my:map-to-object($map)"/>

The main advantage is it avoids the dynamic eval. But getting the single and double quotes right is a bit of a nightmare.

Michael Kay
Saxonica

> On 4 Sep 2020, at 22:53, Martynas Jusevičius <martynas-tyFqBaoSXPC1Z/[email protected]> wrote:
> 
> Right, the attribute is a leftover from the Saxon-CE code... I found
> this snippet somewhere, I think the intention is to be able to use the
> attribute value template to inject values. But I can see that || in
> XSLT 3 does pretty much the same.
> 
> This code is calling methods on a JS library which builds a SPARQL
> query string in the end. It all works fine through ixsl:call(), it's
> just the object creation that requires ixsl:eval().
> 
> On Fri, Sep 4, 2020 at 11:45 PM Michael Kay <mike-JkSD5nQpfvpWk0Htik3J/[email protected]> wrote:
>> 
>> I can't see why you want to create an attribute in order to extract its string value. Why not just construct the string in the first place?
>> 
>> <xsl:variable name="js-statement" as="xs:string"
>> select="{ subject:  SPARQLBuilder.SelectBuilder.var('child'),
>>                predicate: SPARQLBuilder.SelectBuilder.var(' || $predicate || '),
>>                object: SPARQLBuilder.SelectBuilder.var('whatever') }"/>
>> 
>> Apart from that, if you really need to write the application by generating Javascript dynamically and then evaluating it, then I don't think it's going to much simpler than this. But I can't really judge whether that is actually the right design.
>> 
>> Michael Kay
>> Saxonica
>> 
>> 
>>> "
>>> select="ixsl:eval()"/>
>> 
>>> On 4 Sep 2020, at 21:39, Martynas Jusevičius <martynas-tyFqBaoSXPC1Z/[email protected]> wrote:
>>> 
>>> Hi,
>>> 
>>> Is there a quicker/cleaner way to create a JSON object and store it in
>>> a variable than this?
>>> 
>>>       <xsl:variable name="js-statement" as="element()">
>>>           <root statement="{{ subject:
>>> SPARQLBuilder.SelectBuilder.var('child'), predicate:
>>> SPARQLBuilder.SelectBuilder.var('{$predicate}'), object:
>>> SPARQLBuilder.SelectBuilder.var('whatever') }}"/>
>>>       </xsl:variable>
>>>       <xsl:variable name="bgp"
>>> select="ixsl:eval(string($js-statement/@statement))"/>
>>> 
>>> $bgp is later being used like this:
>>> 
>>> 
>>> _______________________________________________
>>> saxon-help mailing list archived at http://saxon.markmail.org/
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/saxon-help
>> 
>> 
>> 
>> _______________________________________________
>> saxon-help mailing list archived at http://saxon.markmail.org/
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/saxon-help
> 
> 
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/saxon-help

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[email protected]
https://lists.sourceforge.net/lists/listinfo/saxon-help
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.