sem.sparql( 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.
Parameters | |
---|---|
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:
|
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
If a |
http://marklogic.com/xdmp/privileges/sem-sparql
The options parse-check
and prepare
cannot be
used at the same time.
declareUpdate(); var sem = require("/MarkLogic/semantics.xqy"); // load an rdf triple that will match the SPARQL query :) sem.rdfInsert( sem.triple(sem.iri("http://www.example.org/dept/108/invoices/20963"), sem.iri("http://www.example.org/dept/108/invoices/paid"), "true")) ; // returns the URI of the document that contains the triple ****** // run SPARQL against above triple sem.sparql('\n\ PREFIX inv: <http://www.example.org/dept/108/invoices/>\n\ \n\ SELECT ?predicate ?object\n\ WHERE \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": sem.iri("http://www.example.org/dept/108/invoices/20963")}; sem.sparql("\n\ SELECT ?predicate ?object\n\ WHERE\n\ { ?subject ?predicate ?object } ", params); // returns the predicate(s) and objects(s) that match the query { "predicate":"<http://www.example.org/dept/108/invoices/paid>", "object":"true" }
declareUpdate(); 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>\ WHERE\ {\ ?product rdf:type p2:shirt ;\ p2:color "blue"\ }'; sem.sparql( sparqlQuery, [], [], rdfsStore ) //returns the triples that match the query { "product": "http://example.org/products/1001" } { "product": "http://example.org/products/1002" }
declareUpdate(); var sem = require("/MarkLogic/semantics.xqy"); // load three rdf triples, two of which will match the SPARQL query sem.rdfInsert( sem.triple(sem.iri("http://www.example.org/dept/108/invoices/20963"), sem.iri("http://www.example.org/dept/108/invoices/paid"), "true")) ; sem.rdfInsert( sem.triple(sem.iri("http://www.example.org/dept/108/invoices/99887"), sem.iri("http://www.example.org/dept/108/invoices/paid"), "true")) ; sem.rdfInsert( sem.triple(sem.iri("http://www.example.org/dept/108/invoices/11111"), sem.iri("http://www.example.org/dept/108/invoices/paid"), "true")) ; // returns the URI of the document that contains the triple // Run this sparql to select two of the triples using an array of IRIs in the binding. let binding = {invoices: [ sem.iri("http://www.example.org/dept/108/invoices/20963"), sem.iri("http://www.example.org/dept/108/invoices/99887") ] } sem.sparql(` SELECT ?predicate ?object WHERE { ?inv ?predicate ?object FILTER (?inv = ?invoices) } `, binding ); //returns the triples that match the query { "predicate": "http://www.example.org/dept/108/invoices/paid", "object": "true" } { "predicate": "http://www.example.org/dept/108/invoices/paid", "object": "true" }
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.