Loading TOC...
Semantics Developer's Guide (PDF)

Semantics 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 will 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.

Let's assume that the data contains a subject node with the resource 'André-Marie Ampère' entered as 'Andr%C3%A9-Marie_Amp%C3%A8re':

<sem:triples xmlns:sem="http://marklogic.com/semantics">
  <sem:subject>http://dbpedia.org/resource/Andr%C3%A9-Marie_
    Amp%C3%A8re
  </sem:subject>
  <sem:predicate>http://www.w3.org/1999/02/22-rdf-syntax-ns#type/
  </sem:predicate>
  <sem:object>http://xmlns.com/foaf/0.1/Person/ 
  </sem:object>
</sem:triples>

In this next example, the sem:rdf-builder function constructs a triple and passes this to the sem:database-nodes function. A new $replace triple is created and passed to the xdmp:node-replace function.

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

let $builder := sem:rdf-builder(sem:prefixes("dbpedia:
http://dbpedia.org/resource/")) 

let $triple := $builder("dbpedia:Andr%C3%A9-Marie_Amp%C3%A8re", "a",
"foaf:Person")

let $node := sem:database-nodes($triple) 

let $replace := 
<sem:triple>
<sem:subject>http://dbpedia.org/resource/André-Marie_Ampère/
</sem:subject>
{$node[1]/sem:predicate, $node[1]/sem:object}
</sem:triple>

return $node ! xdmp:node-replace(., $replace);

Only the triple bound to the variable $builder in the example matches the expression and is updated (replaced).

In Server-Side JavaScript, the example would look very similar.

declareUpdate();
var sem = require("/MarkLogic/semantics.xqy");
var builder = sem.rdfBuilder();
var triple = xdmp.apply(builder, "dbpedia:André-Marie_Ampère",
  "a","foaf:Person");
var node = sem.databaseNodes(triple);

var replace = new NodeBuilder();
replace.startElement("sem:triple","http://marklogic.com/semantics/");
replace.addElement("sem:subject","http://dbpedia.org/resource/André-
  Marie_Ampère/","http://marklogic.com/semantics");
replace.addNode(node.getElementsByTagNameNS("http://marklogic.com/
  semantics","predicate")[0]);
replace.addNode(node.getElementsByTagNameNS("http://marklogic.com/
  semantics","object")[0]);
replace.endElement();
for (var node of nodes) {
xdmp.nodeReplace(node, replace);
};

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 'Andr%C3%A9-Marie_Amp%C3%A8re' in the subject position and replaces each occurrence:

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/Andr%C3%A9-Marie_Amp%
    C3%A8re"),()())
for  $triple in  $triples
let $node := sem:database-nodes($triple)
let $replace := 
    <sem:triple>
       <sem:subject>http://dbpedia.org/resource/André-Marie_Ampère
</sem:subject>
       {$node/sem:predicate, $node/sem:object}
    </sem:triple>
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:

cts:triples(sem:iri("http://dbpedia.org/resource/
André-Marie_Ampère"),
         ()())

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:

sem:graph-delete

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";

sem:graph-delete(sem:iri("mynamedgraph"))

In Server-Side JavaScript the command would be:

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

sem.graphDelete(sem.iri("mynamedgraph"))

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

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

sem:graph-delete(sem:iri("http://marklogic.com/semantics#default-graph
"))

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

xdmp:node-delete

To delete a set of triples from the database, use the sem:database-nodes function with xdmp:node-delete. This function will work 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 :=
cts:triples(sem:iri("http://www.rdfabout.com/rdf/usgov/congress/people
/D000596"),()())

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:

var sem = require("/MarkLogic/semantics.xqy");

let $triples :=
cts.triples(sem.iri("http://www.rdfabout.com/rdf/usgov/congress/people
/D000596"),()())

for $triple in $triples
return (sem.databaseNodes($triple) ! xdmp.nodeDelete(.))
xdmp:document-delete

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.

For example:

xquery version "1.0-ml";

xdmp:document-delete("example.xml")

The xdmp:document-delete function deletes the documents and properties. This document also deletes all the triples embedded in that document. To delete all documents in a directory, use the xdmp:directory-delete function.

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

var sem = require("/MarkLogic/semantics.xqy");

xdmp.documentDelete("example.xml")

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:

http://<host:port>/<version>/graphs?graph=graph-iri

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 \
http://localhost:8321/v1/graphs?default

Caution should be used 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 \
http://localhost:8321/v1/graphs?graph=http://marklogic.com/semantics#mygraph/

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'
    "http://localhost:8033/v1/graphs?graph=mynamed-graph"

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
Powered by MarkLogic Server 7.0-4.1 and rundmc | Terms of Use | Privacy Policy