SPARQL and XQuery/JavaScript Together

In this exercise, you'll learn how to use SPARQL and either XQuery or JavaScript together in a few different ways. We'll write queries on the BBC news documents and related triples we loaded in the second exercise. We will provide examples in both XQuery and JavaScript. When referring to MarkLogic-specific functions, we'll include the JavaScript name of the function in parentheses. For example, "sem:sparql (sem.sparql)" refers to the XQuery sem:sparql function and the JavaScript version of that same function is called sem.sparql.

We have provided versions of these queries in the associated Query Console workspace, sparql-xquery.xml.

Using sem:sparql (sem.sparql)

From Query Console, use query type "XQuery" or "JavaScript" against your "tutsem-content" database. When you run in XQuery, wrap the SPARQL with sem:sparql(). The sem:sparql XQuery function is a built-in function so you don't have to import a library to use it.

When you run in JavaScript, wrap the SPARQL with sem.sparql(), and don't forget:

For example, the following simple query for people born in Brooklyn can be written in XQuery or JavaScript:

Passing parameters from XQuery or JavaScript into SPARQL

If you recall from our previous exercise, for a specific IRI, we can find all the subjects added by OpenCalais and organize them by type with a SPARQL query like:

But, let's say we'd like to do this for an arbitrary document. The XQuery sem:sparql or JavaScript sem.sparql function takes a second parameter, which is a map of parameter bindings. We can use it to pass in an IRI like this:

Neato!

Now, if you know how to use MarkLogic XQuery or JavaScript search APIs, you should be able to find all news items with "Elton John" in their title and show their OpenCalais subjects. Try this on your own (the answer is in the provided sparql-xquery.xml workspace).

Restricting a SPARQL with a cts:query (cts.query)

The call, sem:sparql in XQuery or sem.sparql in JavaScript, actually takes several arguments beyond the bindings we used above. In particular, you can use the fourth argument to constrain the query.

Let's say you're interested in cities. First you can find all the cities mentioned in the news like this:

But now, say you'd like to restrict that to city names mentioned in certain articles. Here, let's find all the city names referenced in articles that have the phrase "dark magic". We can constrain the SPARQL query as:

CONSTRUCT new triples and insert them with XQuery or JavaScript

OK, now let's go the other way, taking something from SPARQL and passing it into either XQuery or JavaScript.

Let's use SPARQL CONSTRUCT to construct triples and then XQuery's xdmp:node-insert-child() or JavaScript's xdmp.nodeInsertChild() to insert them into a document. (Yes, you can embed triples directly in documents ourselves if you want).

Yes, it's that easy to take the output of a SPARQL query and use it in XQuery or JavaScript.

Serializing SPARQL results as XML, JSON, or triples

What formats does sem:sparql() or sem.sparql() return? You can try each of the different kinds of SPARQL queries (SELECT, CONSTRUCT, ASK, DESCRIBE) and wrap them in xdmp:describe() or xdmp.describe() to see the format of the result. There are queries in the associated workspace for this, but in case you're too impatient to click over, here's what you'll get:

Query Return
SELECT sequence of JSON objects
CONSTRUCT sequence of sem:triple()
ASK boolean
DESCRIBE sequence of sem:triple()

There are a couple other useful functions for serializing RDF and SPARQL query results that you can use:

As an exercise, you can use these to format the results of each kind of SPARQL query as RDF (where applicable), XML, or JSON.

References

Query Console Workspace ts-sparql-xquery.xml.

Introducing SPARQL

Using the Triple Index

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.