sem:sparql( $sparql as xs:string, [$bindings as map:map?], [$options as xs:string*], [$store as sem:store*] ) as item()*
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.
xquery version "1.0-ml"; import module namespace sem = "http://marklogic.com/semantics" at "/MarkLogic/semantics.xqy"; (: load an rdf triple that will match the SPARQL query :) sem:rdf-insert( 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 :) sem:sparql(' PREFIX inv: <http://www.example.org/dept/108/invoices/> SELECT ?predicate ?object WHERE { inv:20963 ?predicate ?object } ') (: returns the predicate(s) and object(s) for the matching triple(s) :)
xquery version "1.0-ml"; (: this query uses the data from the previous query and shows how to pass bindings in a parameter to sem:sparql :) let $params := map:new(map:entry("subject", sem:iri("http://www.example.org/dept/108/invoices/20963"))) return sem:sparql(" SELECT ?predicate ?object WHERE { ?subject ?predicate ?object } ", $params) (: returns the predicate(s) and object(s) for the matching triple(s) :) [{"predicate":"<http://www.example.org/dept/108/invoices/paid>", "object":"\"true\""}]
xquery version "1.0-ml"; import module namespace sem = "http://marklogic.com/semantics" at "/MarkLogic/semantics.xqy"; (: load sample data triples and ontology triple in turtle format :) sem:rdf-insert(sem:rdf-parse(' @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/> . 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 .', ("graph=graph-1", "turtle"))); (: create a store that uses an RDFS ruleset for inferencing :) let $rdfs-store := sem:ruleset-store("rdfs.rules",sem:store() ) return (: use the store you just created - pass it in to sem:sparql() :) sem:sparql(' 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" }', (), (), $rdfs-store ) (: returns the triples you inserted and the triples that match the query :) /triplestore/5766e26a98b19b21.xml product <http://example.org/products/prod:1001> <http://example.org/products/prod:1002> }]