Search code examples
xquerymarklogictriplestoretriples

How to construct triples manually in Marklogic


I have been trying to insert triples in Marklogic using this query

 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>

Unfortunately, that returns a coercion error:

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

What am I doing wrong?


Solution

  • You should not put quotes around sem:triple and sem:iri, they are type cast functions:

    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>
    

    If you are trying to create the triples dynamically, pass through a sem:triple or sem:iri objects from external, or pass through the string values, and cast them inside the code.

    HTH!