使用XPath 3.1 fn:serialize进行JSON序列化 [英] JSON serialization with XPath 3.1 fn:serialize
问题描述
我正在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 解释了 因此,基本上,如果您在该映射中列出的字符被映射为自身,则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 The I can remove the escape characters with An example stylesheet: Without Using Saxon 9.8, if the See the spec https://www.w3.org/TR/xpath-functions-31/#func-serialize explaining the second argument to 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屋!serialize
的第二个参数是map
,其中use-character-maps
本身是map(xs:string, xs:string)?
,而键是要映射的字符(作为xs:string实例),其对应值是要替换这些字符的字符串"和
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? 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
. fn:replace
, but would like to know whether there is a built-in way to do it that I am missing.<?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
, the result is {"@context":"http:\/\/schema.org"}
. With fn:replace
, the result is {"@context":"http://schema.org"}
.serialize
function is called as => serialize(map {'method': 'json', 'use-character-maps' : map { '/' : '/' }})
, the solidus is output as is and not escaped as \/
.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.".