如何在Marklogic中手动构造三元组

时间:2015-07-13 06:03:30

标签: xquery marklogic triplestore triples

我一直在尝试使用此查询在Marklogic中插入三元组

 xquery version "1.0-ml";
 import module namespace sem = "http://marklogic.com/semantics"
  at "/MarkLogic/semantics.xqy";

 declare variable $TRIPLE as xs:string external ;
 declare variable $GRAPHNAME as xs:string external ;
 let $TRIPLE:="sem:triple(sem:iri('http://smartlogic.com/document#testForTriples.xml'),sem:iri('http://www.smartlogic.com/schemas/docinfo.rdf#type'),'document')"
 let $GRAPHNAME :="sem:iri('testGraph')"
 let $r :=
 sem:graph-insert($GRAPHNAME,  $TRIPLE)

 return <result>{$r}</result>

不幸的是,这会返回强制错误:

XDMP-AS: (err:XPTY0004) $graphname as sem:iri -- Invalid coercion: "sem:iri('testGraph')" as sem:iri

我做错了什么?

1 个答案:

答案 0 :(得分:1)

你不应该在sem:triplesem:iri附近加上引号,它们是类型转换函数:

xquery version "1.0-ml";

import module namespace sem = "http://marklogic.com/semantics"
  at "/MarkLogic/semantics.xqy";

let $TRIPLE :=  sem:triple(sem:iri('http://smartlogic.com/document#testForTriples.xml'),sem:iri('http://www.smartlogic.com/schemas/docinfo.rdf#type'),'document')
let $GRAPHNAME := sem:iri('testGraph')
let $r := sem:graph-insert($GRAPHNAME,  $TRIPLE)

return <result>{$r}</result>

如果您尝试动态创建三元组,请通过sem:triple或sem:iri来自外部的对象,或者传递字符串值,并将它们转换为代码。

HTH!