Loading TOC...
Semantic Graph Developer's Guide (PDF)

Semantic Graph Developer's Guide — Chapter 12

Inserting, Deleting, and Modifying Triples with XQuery and Server-Side JavaScript

Triples can be modified with XQuery or Server-side JavaScript, using MarkLogic xdmp built-ins. Triples managed by MarkLogic - those triples having a document root of sem:triples - can be modified using SPARQL Update. See Using SPARQL Update for more information about modifying managed triples.

Unmanaged triples, those triples embedded in another document with an element node of sem:triple, can only be modified using XQuery or Server-Side JavaScript and xdmp built-ins. To perform updates on triples in your datastore (for either managed or unmanaged triples), you insert a new triple and delete the existing one. You are not updating the existing triple; the update operation is actually an INSERT/DELETE procedure.

This chapter includes the following sections:

Updating Triples

You can use XQuery or Server-Side JavaScript functions to update existing triples in a database, by using INSERT/DELETE to replace nodes. For a managed triple, the sem:database-nodes (sem.databaseNode in Server-Side JavaScript) and the xdmp:node-replace (xdmp.nodeReplace in Server-Side JavaScript) functions are used to correct inaccurate data.

Assume the database contains a document containing the following unmanaged triple, with the resource John Doe entered as John_Doe:

<sem:triples xmlns:sem="http://marklogic.com/semantics">

The following example replaces the subject with http://dbpedia.org/resource/John_Doe using the xdmp:node-replace function. The example uses sem:rdf-builder to construct a triple that matches the one we want to change. Using this triple with sem:database-nodes finds the matching nodes in the database to be changed.

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

(: construct the triple to match against :)
let $builder := sem:rdf-builder(
  sem:prefixes("dbpedia: http://dbpedia.org/resource/"))
let $triple := $builder(
  "dbpedia:John_Doe", "a", "foaf:Person")
(: find matching unmanaged triples in the database :)
let $node := sem:database-nodes($triple)
(: construct the replacement triple with a new subject :)
let $replace := 
    {$node[1]/sem:predicate, $node[1]/sem:object}

(: replace the old triple with the new one in all matched nodes :)
return $node ! xdmp:node-replace($node, $replace);

The following example performs the same operation using Server-Side JavaScript. The example uses the NodeBuilder interface to construct the replacement node.

const sem = require('/MarkLogic/semantics');

// construct the triple to find in the database
const builder = 
  sem.rdfBuilder(sem.prefixes('dbpedia: http://dbpedia.org/resource/'));
const triple = xdmp.apply(builder, 
  'dbpedia:John_Doe', 'a', 'foaf:Person');

for (let node of sem.databaseNodes(triple)) {
  // construct the replacement triple with the new subject
  const pred = fn.head(
               {'sem': 'http://marklogic.com/semantics'}));
  const obj = fn.head(
               {'sem': 'http://marklogic.com/semantics'}));
  const replacement = new NodeBuilder()
  // replace the old triple with the new one
  xdmp.nodeReplace(node, replacement)

When you have multiple triples to update, you can use XQuery or Server-Side JavaScript (or if they are managed triples, SPARQL Update), to find matching triples, and then iterate over the nodes to replace them.

In this example, a cts:triples call finds all triples with John_Doe in the subject position and replaces each occurrence with Jane_Roe:

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

let $triples := cts:triples(sem:iri("http://dbpedia.org/resource/John_Doe"),(),())
for $triple in  $triples
let $node := sem:database-nodes($triple)
let $replace := 
       {$node/sem:predicate, $node/sem:object}
return $node ! xdmp:node-replace(., $replace)

An empty sequence is returned for both of the examples because the replacements have been made. Use a simple cts:triples call to verify that the updates have been made:


Using the xdmp:node-replace function results in creating a new fragment and deleting the old fragment. When the system performs a merge, the deleted fragments are removed permanently. The system performs automatic merges, unless this feature has been disabled by an administrator.

Deleting Triples

This section discusses methods for deleting RDF data in MarkLogic and includes the following topics:

Deleting Triples with XQuery or Server-Side JavaScript

There are several functions you can use to delete triples from a database. This section discusses the following functions:


This function only works for managed triples.

You can use the sem:graph-delete function to delete all managed triple documents in a named graph. You specify the graph IRI as the parameter.

For example:

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


In Server-Side JavaScript the command would be:

const sem = require("/marklogic/semantics.xqy");


The following example deletes all managed triples in the default graph. If no other named graphs exist, this might remove all triples from the database:

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


The sem:graph-delete function will only delete triples inserted by the Graph Store API, which have a document root element of sem:triple. If you delete a specific named graph, it will not affect documents with embedded triples (with a sem:triples element node), so the graph might still exist.


To delete a set of triples from the database, use the sem:database-nodes function with xdmp:node-delete in XQuery, or sem.databaseNodes with xdmp.nodeDelete in Server-Side JavaScript. This function works with managed or unmanaged triples.

For example:

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

let $triples :=

for $triple in $triples
return (sem:database-nodes($triple) ! xdmp:node-delete(.))

This query will not delete empty sem:triple document elements if all the triples are deleted from a single document.

In Server-Side JavaScript, the example would look like this:

const sem = require('/MarkLogic/semantics');
const triples = cts.triples(
  null, null);

for (let triple of triples) {
  for (let node of xdm.databaseNodes(triple)) {

You can remove documents containing triples from the database with the xdmp:document-delete function. This function deletes a document and all of its properties, and works with both managed and unmanaged triples. Specify the IRI of the document to be deleted as the parameter.

The following XQuery example deletes the document with URI example.xml:

xquery version "1.0-ml";

The following example performs the equivalent operation in Server-Side JavaScript:

const sem = require('/MarkLogic/semantics.xqy');


Deleting a document deletes the document, any triples embedded in the document, and the document properties.

To delete all documents in a directory, use the xdmp:directory-delete function.

Deleting Triples with REST API

You can use the REST API to delete triples in the default graph or a named graph by sending a DELETE request to the DELETE /v1/graphs service. To delete triples from a named graph, use curl to send the DELETE request in the following form:


where graph-iri is the IRI of your named graph.

The IRI for the named graph in the request is http://host:port/version/graphs?default. For example, this DELETE request removes all triples from the default graph at port 8321:

#Windows users, see Modifying the Example Commands for Windows 

$ curl --anyauth --user user:password -X DELETE \

Use caution when specifying the graph, since there is no confirmation check before deleting the dataset.

This curl command will delete the triples in the graph named mygraph.

#Windows users, see Modifying the Example Commands for Windows 

$ curl --anyauth --user user:password -X DELETE \

As with the sem:graph-delete function, the DELETE request removes triples from graphs where sem:triples is the root element of the containing document (managed triples). XML documents that contain embedded triples are unaffected. Graphs may still exist after the DELETE operation if the graph contained both types of documents.

When you send a PUT request, triples are replaced in a named graph or added to an empty graph if the graph did not exist. This is the equivalent of a DELETE followed by POST.

For example:

# Windows users, see Modifying the Example Commands for Windows 

$ curl --digest --user admin:password -s -X PUT 
-H "Content-type:text/turtle" --data-binary '@./example.ttl'

To perform the equivalent of a DELETE operation using the REST API, use curl to send a PUT request with an empty graph.

« Previous chapter
Next chapter »