Loading TOC...

MarkLogic 9 Product Documentation
sem:sparql

sem:sparql(
   $sparql as xs:string,
   [$bindings as map:map?],
   [$options as xs:string*],
   [$store as sem:store*]
) as item()*

Summary

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:
"base=IRI"
The initial base IRI for the query.
"default-graph=IRI*"
Add the named graph or graphs specified by the IRI to the default graph for the query.
"named-graph=IRI*"
Add the named graph or graphs specified by the IRI to the list of named graphs available in the query.
"parse-check"
Parse the query, but don't perform static checks or execute it. The default is false.
"prepare"
Parse and optimize the query, caching the result. No execution is performed. Default is false.
"optimize=N"
Sets the optimization level to use. Levels of 0 (off), 1, and 2 are recognized. The default is 1.
"dedup=N"
Sets de-duplication of triples results to "on" or "off". The default, standards-compliant behavior is "on".

array

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

map

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

http://marklogic.com/xdmp/privileges/sem-sparql

Usage Notes

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

Example

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)
 :)

Example

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\""}]

Example

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>
}]
 

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