JSON serialization with XPath 3.1 fn: serialization

I am using XSLT 3.0 in Saxon-HE 9.8 and would like to work with JSON documents as bound data in JSON-LD . Full HTTP URIs often appear as values ​​in JSON-LD.

When I use XPath 3.1 fn:serialize

to round the data to JSON, Solidus characters are http://

escaped. Is it possible to avoid this speedup when serializing back to JSON?

The function fn:parse-json

has a parameter escape

that can be set to true()

or false()

, but I don't see anything like that for fn:serialize

.

I can remove the escape characters with fn:replace

, but would like to know if there is a built-in way to do this that I am missing.

Style sheet example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">

    <xsl:output omit-xml-declaration="yes"/>

    <xsl:variable name="j" expand-text="no"> { "@context": "http://schema.org" } </xsl:variable>

    <xsl:template name="init">
        <xsl:sequence
            select="            
                $j => parse-json(map {'escape': false(), 'liberal': true()})
                => serialize(map {'method': 'json'})
                => replace('\\/', '/')
            "/>
    </xsl:template>

</xsl:stylesheet>

      

No fn:replace

result {"@context":"http:\/\/schema.org"}

. With the fn:replace

result {"@context":"http://schema.org"}

.

+3


source to share


1 answer


Using Saxon 9.8, if a function serialize

is called as => serialize(map {'method': 'json', 'use-character-maps' : map { '/' : '/' }})

, Solidus is deduced as is and is not escaped as \/

.

See the spec https://www.w3.org/TR/xpath-functions-31/#func-serialize explaining the second argument serialize

as map

, where is use-character-maps

itself map(xs:string, xs:string)?

, and "keys are the characters to be displayed (like xs: string instances), and the corresponding values ​​are the strings to be replaced for those characters "and 3.1 serialization specification :" Any character in a string that has a character mapping defined ... is replaced with a replacement string defined in a character map. " and "Any other character in the input string (but not a character generated by character matching) is a candidate for [...] JSON disabling."



So, basically, if you list the characters in this map, being mapped to itself, the JSON encoding will not modify them further.

+2


source







All Articles