我试图使用与访问XPath相同的技术将JSON转换成JSON。下面是两个相同XPath应用的等效结构。
let $json := xdmp:unquote('{
"foo": {
"bar": {
"bas": "findme",
"boo": "324"
}
}
}')
let $xml := <xml>
<foo>
<bar>
<bas>findme</bas>
<boo>324</boo>
</bar>
</foo>
</xml>
return (
$xml//node()[./text() = "findme"],
$json//node()[./text() = "findme"]
)我希望两者都能得到同样的结果,但我得到了以下结果:
XML结果
<bas>findme</bas>
JSON结果
{ "bas": "findme", "boo": "324" }
为什么不产生同样的结果呢?
发布于 2017-02-09 13:49:24
在MarkLogic中,text属性bas是一个命名的文本节点,它在XML中不存在。它的设计使得像//bas这样的东西对两者都适用。由于命名的文本节点,树结构在最深的层次上是不同的:
element bas {
text { "findme" }
},
element boo {
text { "324" }
}相对于:
object-node {
text bas { "findme" },
text boo { "324" }
}注:后者为伪码.正确使用JSON构造函数应该是:object-node { "bas": "findme", "boo": "324" }。
通过使用fn:name() (顺便说一句,fn:local-name()在这里不起作用),也许有一种方法可以更接近您所追求的目标。试一试如下:
let $json := xdmp:unquote('{
"foo": {
"bar": {
"bas": "findme",
"boo": "324"
}
}
}')
let $xml := <xml>
<foo>
<bar>
<bas>findme</bas>
<boo>324</boo>
</bar>
</foo>
</xml>
let $xml-text := $xml//text()[. = "findme"]
let $json-text := $json//text()[. = "findme"]
for $t in ($xml-text, $json-text)
return
if (name($t) eq "") then
$t/..
else
object-node { name($t): $t }哈哈!
https://stackoverflow.com/questions/42136732
复制相似问题