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

Debbie Lockett <debbie-JkSD5nQpfvpWk0Htik3J/[email protected]> Mon, 14 Sep 2020 16:18:51 +0100
Newsgroups gmane.text.xml.saxon.help
Message-ID <[email protected]>
Belated answer to this question: currently generally the best you can do 
is specify `as="item()*"` for JS Objects, which clearly isn't very 
useful. I've just added an issue to the Saxon-JS project to address this 
- we should add syntax for an external JavaScript object type. See 
https://saxonica.plan.io/issues/4736.


On 07/09/2020 09:34, Martynas Jusevičius wrote:
> How about storing JS objects in <xsl:variable> (e.g. results of
> ixsl:call()) -- what should the as="" attribute value be?
>
> On Sat, Sep 5, 2020 at 12:49 AM Michael Kay <[email protected]> wrote:
>> 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 <[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 <[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 <[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
>
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/saxon-help

-- 
Debbie Lockett
Saxonica



_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[email protected]
https://lists.sourceforge.net/lists/listinfo/saxon-help