使用XPath 3.1 fn:serialize进行JSON序列化 [英] JSON serialization with XPath 3.1 fn:serialize

查看:100
本文介绍了使用XPath 3.1 fn:serialize进行JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Saxon-HE 9.8中使用XSLT 3.0,并希望将JSON文档作为 JSON-LD中的链接数据使用.在JSON-LD中,完整的HTTP URI通常作为值出现.

当我使用XPath 3.1 fn:serialize将数据往返返回JSON时,会转义http://中的固线字符.序列化回JSON时是否可以避免这种转义?

fn:parse-json函数具有一个可以设置为true()false()escape参数,但是我没有看到与fn:serialize类似的东西.

我可以使用fn:replace删除转义字符,但是想知道是否有内置的方法来丢失我.

样式表示例:

<?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>

没有fn:replace,结果为{"@context":"http:\/\/schema.org"}.对于fn:replace,结果为{"@context":"http://schema.org"}.

解决方案

使用Saxon 9.8,如果serialize函数称为=> serialize(map {'method': 'json', 'use-character-maps' : map { '/' : '/' }}),则固相线将按原样输出,而不会作为\/逸出. >

请参阅规范 https://www.w3.org/TR/xpath-functions-31/#func-serialize 解释了serialize的第二个参数是map,其中use-character-maps本身是map(xs:string, xs:string)?,而键是要映射的字符(作为xs:string实例),其对应值是要替换这些字符的字符串"和

因此,基本上,如果您在该映射中列出的字符被映射为自身,则JSON编码不会进一步改变它们.

I am using XSLT 3.0 in Saxon-HE 9.8 and would like to work with JSON documents as linked data in JSON-LD. In JSON-LD, full HTTP URIs often appear as values.

When I use the XPath 3.1 fn:serialize to round-trip the data back to JSON, the solidus characters in http:// are escaped. Is it possible to avoid this escaping when serializing back to JSON?

The fn:parse-json function has an escape parameter that can be set to true() or false(), but I don't see anything similar for fn:serialize.

I can remove the escape characters with fn:replace, but would like to know whether there is a built-in way to do it that I am missing.

An example stylesheet:

<?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>

Without fn:replace, the result is {"@context":"http:\/\/schema.org"}. With fn:replace, the result is {"@context":"http://schema.org"}.

解决方案

Using Saxon 9.8, if the serialize function is called as => serialize(map {'method': 'json', 'use-character-maps' : map { '/' : '/' }}), the solidus is output as is and not escaped as \/.

See the spec https://www.w3.org/TR/xpath-functions-31/#func-serialize explaining the second argument to serialize being a map where the use-character-maps is itself a map(xs:string, xs:string)? and the "keys are the characters to be mapped (as xs:string instances), and whose corresponding values are the strings to be substituted for these characters" and the 3.1 serialization spec saying "Any character in the string for which character mapping is defined ... is substituted by the replacement string defined in the character map." and "Any other character in the input string (but not a character produced by character mapping) is a candidate for [...] JSON escaping.".

So basically if you list characters in that map as being mapped to themselves the JSON encoding will not alter them further.

这篇关于使用XPath 3.1 fn:serialize进行JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆