Loading TOC...


   sparql as String,
   [bindings as Object?],
   [options as String[]],
   [store as sem.store[]]
) as Sequence


Executes a SPARQL query against the database.

SPARQL "SELECT" queries return a solution as a sequence of map objects in the form of a table, where each map represents a set of bindings that satisfies the query.

SPARQL "CONSTRUCT" queries return triples as a sequence of sem:triple values in an RDF graph.

SPARQL "DESCRIBE" queries return a sequence of sem:triple values as an RDF graph that describes the resources found by the query.

SPARQL "ASK" queries return a single xs:boolean value (true or false) indicating whether a query pattern matches in the dataset.

This function is a built-in.

sparql The SPARQL query to be executed.
bindings A map containing initial values for variables from the query, or the empty sequence if no query variables are to be initially bound. This is a way to parameterize the query.
options Options as a sequence of string values. Available options are:
The initial base IRI for the query.
Add the named graph or graphs specified by the IRI to the default graph for the query.
Add the named graph or graphs specified by the IRI to the list of named graphs available in the query.
Parse the query, but don't perform static checks or execute it. The default is false.
Parse and optimize the query, caching the result. No execution is performed. Default is false.
Sets the optimization level to use. Levels of 0 (off), 1, and 2 are recognized. The default is 1.
Sets de-duplication of triples results to "on" or "off". The default, standards-compliant behavior is "on".


Return the result as a sequence of array values (json:array).


Return the result as a sequence of map values, where the key is the column name (sem:binding).
store A sem:store constructor to use as the source of the triples for the SPARQL query. If multiple sem:store constructors are supplied, the triples from all the sources are merged and queried together. The default for sem:store is the current database's triple index, restricted by its options and query argument (for instance, "triples in documents matching this query").

Options for "any", "document", "properties", "locks", "checked", or "unchecked", which used to be part of the sem:sparql signature, must be specified as part of sem:store, not as part of sem:sparql. The sem:sparql query will run with the locking option specified in sem:store. Locking is ignored in a query transaction.

If a sem:store value is not supplied, then the default sem:store for the statement will be used. This is the same as calling sem:store() with all arguments omitted, which will access the current database's triple index, using the default rulesets configured for that database. The default for locking is read-write.

Required Privileges


Usage Notes

The options parse-check and prepare cannot be used at the same time.


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

// load an rdf triple that will match the SPARQL query :)

             "true")) ;
// returns the URI of the document that contains the triple


// run SPARQL against above triple

PREFIX inv: <http://www.example.org/dept/108/invoices/>\n\
SELECT ?predicate ?object\n\
{ inv:20963 ?predicate ?object }\n\

// returns the predicate(s) and object(s) for the matching triple(s)
  "predicate": "http://www.example.org/dept/108/invoices/paid",
  "object": "true"


//   this query uses the data from the previous query and shows how to
//   pass bindings in a parameter to sem:sparql

var params = {"subject":
  SELECT ?predicate ?object\n\
  { ?subject ?predicate ?object } ",

// returns the predicate(s) and objects(s) that match the query


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

// load sample data triples and ontology triple
var string ='\
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\
@prefix p0: <http://www.example.org/products/> .\
@prefix p2: <http://www.example.com/> .\
p2:Henley       <http://www.w3.org/2000/01/rdf-schema#subClassOf>\
                                p2:shirt .\
p0:prod:1001    p2:color        "blue" ;\
                a               p2:Henley .\
p0:prod:1002    p2:color        "blue" ;\
                a               p2:shirt .\
sem.rdfInsert(sem.rdfParse(string, ["graph=graph-1", "turtle"]))

// create a store that uses an RDFS ruleset for inference
var rdfsStore = sem.rulesetStore("rdfs.rules", sem.store());

// use the store you just created - pass it into sem.sparql()
var sparqlQuery = '\
    prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\
    prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\
    prefix p0: <http://www.example.org/products/>\
    prefix p2: <http://www.example.com/>\
    SELECT ?product\
    FROM <graph-1>\
      ?product rdf:type p2:shirt ;\
      p2:color "blue"\
//returns the triples that match the query
"product": "http://example.org/products/1001"
"product": "http://example.org/products/1002"

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