如何以RDF/JSON或Turtle格式获取MarkLogic中的TDE结果?



使用MarkLogic中的任何类型的模板驱动提取(TDE),如何将从tde:node-data-extract函数获得的结果转换为RDF/JSON格式?此方法返回的 JSON 格式与 RDF/JSON 不兼容,因此我不能直接使用它将三元组插入另一个数据库。在这种情况下,我不想将三元组插入到我应用模板的同一数据库中,我只想使用该模板从 XML 数据创建三元组。

以下是我从tde:node-data-extract函数获得的 JSON 输出示例:

{
"document/pt/document/39627370": [{
"triple": {
"subject": "http://www.example.com/document/id/39627370",
"predicate": "http://www.example.com/typeOf",
"object": {
"datatype": "http://www.w3.org/2001/XMLSchema#string",
"value": "http://www.example.com/document"
}
}
},
{
"triple": {
"subject": "http://www.example.com/publisher/Oxford_University_Press",
"predicate": "http://www.example.com/typeOf",
"object": {
"datatype": "http://www.w3.org/2001/XMLSchema#string",
"value": "http://www.example.com/publisher"
}
}
}
}
}

使用sem.triple()将每个 "triple" 属性转换为 triple 对象。然后使用sem.rdfSerialize()序列化sem.triple对象的数组。

  • https://docs.marklogic.com/sem.triple
  • https://docs.marklogic.com/sem.rdfSerialize

在 John 和 Mads 的帮助下,我发现了一个细微的变化,假设你在查询控制台中,效果非常好。$docs是任何文档序列,$template是 TDE 模板。

let $jsontriples := tde:node-data-extract($docs, $template)
for $key in map:keys($jsontriples) 
let $entry := map:get($jsontriples, $key)
return $entry["triple"]

这将在查询控制台"结果"选项卡中返回自动序列化为 Turtle 格式的三元组,您可以将其切换到 JSON 或文本。我认为在序列化未自动执行的情况下(例如,不使用查询控制台时),John 的答案是最正确的。

最新更新