【发布时间】:2015-02-06 19:29:54
【问题描述】:
MarkLogic 8 在许多方面改进了 JSON 支持,但一些 MarkLogic 7 JSON 函数现在具有不同的签名或执行不同的操作。如何编写适用于这两个版本的 XQuery 代码?
【问题讨论】:
MarkLogic 8 在许多方面改进了 JSON 支持,但一些 MarkLogic 7 JSON 函数现在具有不同的签名或执行不同的操作。如何编写适用于这两个版本的 XQuery 代码?
【问题讨论】:
到目前为止,我遇到的主要变化是xdmp:from-json、xdmp:to-json 和json:transfrom-to-json。如果您有大量来自 MarkLogic 7 的 JSON 相关代码,这些更改可能会破坏现有代码。
不要直接调用那些xdmp 和json 函数,而是导入这个库模块并调用c:from-json 等。这使得为MarkLogic 7 编写的代码可以在两个版本中工作。
module namespace c="http://blakeley.com/marklogic/json/compatibility";
import module namespace json="http://marklogic.com/xdmp/json"
at "/MarkLogic/json/json.xqy";
(: No prefix for the fn:* functions :)
declare default function namespace "http://www.w3.org/2005/xpath-functions";
declare variable $VERSION := xs:integer(
substring-before(xdmp:version(), ".")) ;
declare function c:from-json(
$arg as xs:string)
as item()*
{
xdmp:from-json(
if ($VERSION ge 8) then xdmp:unquote($arg, (), "format-json")
else $arg)
};
declare function c:to-json(
$item as item()*)
as xs:string*
{
if ($VERSION ge 8) then xdmp:quote($item)
else xdmp:to-json($item)
};
declare function c:transform-to-json(
$node as node(),
$config as map:map?)
as xs:string
{
json:transform-to-json($node, $config) ! (
if ($VERSION ge 8) then xdmp:quote(.)
else .)
};
declare function c:transform-to-json(
$node as node())
as xs:string
{
c:transform-to-json($node, ())
};
当我遇到其他更改或听说它们时,我会扩展它。如果太长,我会将其移至 gist 或 github 项目。
【讨论】: