<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/sem.sparql.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>sem.sparql</api:function-name><api:suggest>sem.sparql</api:suggest><api:suggest>sem</api:suggest><api:suggest>sparql</api:suggest><api:function-link mode="xquery" fullname="sem:sparql">/apidoc/8.0/sem:sparql.xml</api:function-link><api:function mode="javascript" name="sparql" type="builtin" lib="sem" category="Semantics" hidden="false" bucket="MarkLogic Built-In Functions" prefix="sem" namespace="http://marklogic.com/semantics" fullname="sem.sparql"><api:summary>
  Executes a SPARQL query against the database.
  <p xmlns="http://www.w3.org/1999/xhtml">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.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">SPARQL "CONSTRUCT" queries return triples as a sequence of 
  sem:triple values in an RDF graph.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">SPARQL "DESCRIBE" queries return a sequence of sem:triple values as an RDF
  graph that describes the resources found by the query.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">SPARQL "ASK" queries return a single xs:boolean value (true or false) 
  indicating whether a query pattern matches in the dataset.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">This function is a built-in.</p>
</api:summary><api:params><api:param name="sparql" type="xs:string"><api:param-description>
    The SPARQL query to be executed.
  </api:param-description><api:param-name>sparql</api:param-name><api:param-type>String</api:param-type></api:param><api:param name="bindings" type="map:map?" optional="true"><api:param-description>
    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.
  </api:param-description><api:param-name>bindings</api:param-name><api:param-type>Object?</api:param-type></api:param><api:param name="options" type="xs:string*" optional="true"><api:param-description>
    Options as a sequence of string values. Available options are:
    <dl xmlns="http://www.w3.org/1999/xhtml">
    <dt>"base=<em>IRI</em>"</dt>
    <dd>The initial base IRI for the query.</dd>
    <dt>"default-graph=<em>IRI</em>*"</dt>
    <dd>Add the named graph or graphs specified by the IRI to the default graph for 
    the query.</dd>
    <dt>"named-graph=<em>IRI</em>*"</dt>
    <dd>Add the named graph or graphs specified by the IRI to the list of named graphs 
    available in the query.</dd>
    <dt>"parse-check"</dt>
    <dd>Parse the query, but don't perform static checks or execute it.  
    The default is false.</dd>
    <dt>"prepare"</dt>
    <dd>Parse and optimize the query, caching the result. No execution is 
    performed. Default is false.</dd>
    <dt>"optimize=N"</dt>
    <dd>Sets the optimization level to use. Levels of 0 (off), 1, and 2 are 
    recognized. The default is 1.</dd>
    </dl>
  </api:param-description><api:param-name>options</api:param-name><api:param-type>String[]</api:param-type></api:param><api:param name="store" type="sem:store*" optional="true"><api:param-description>
    A <code xmlns="http://www.w3.org/1999/xhtml">sem:store</code> value to use as the source of the triples for the
    SPARQL query. If multiple <code xmlns="http://www.w3.org/1999/xhtml">sem:store</code> values are supplied, 
    the triples from all the sources are merged and queried together. The 
	default for <code xmlns="http://www.w3.org/1999/xhtml">sem:store</code> is the triples that can be inferred 
	from the rulesets.
	<p xmlns="http://www.w3.org/1999/xhtml">
    Options for "any", "document", "properties", "locks", "checked", or 
    "unchecked" must be specified as part of  
    <a href="./sem:store"><code>sem:store</code></a>, not as part of 
    <code>sem:sparql</code>. When the <code>sem:store</code> parameter is 
    used, these options are invalid for <code>sem:sparql</code>. The locking 
	option specified in <code>sem:store</code> will be used by 
	<code>sem:sparql</code>. The default locking option for <code>sem:store</code> 
	is <code>read-write</code>; read-lock documents containing triples being 
	accessed, write-lock documents being modified. Locking can also be set to 
	<code>write</code>; only write-lock documents being modified. The 
	<code>sem:sparql</code> query will run with the locking option specified in 
	<code>sem:store</code>. Locking is ignored in a query transaction. 
	</p>
	<p xmlns="http://www.w3.org/1999/xhtml"> 
	If <code>sem:store</code> is a <code>cts:query</code>, <code>sem:store</code>
	will use the default locking option (<code>read-write</code>).
	</p>
	<p xmlns="http://www.w3.org/1999/xhtml">
    If a <code>sem:store</code> value is not supplied, then the default 
    <code>sem:store</code> for the statement will be used and the default 
    locking is <code>read-write</code>. This has the same configuration as calling 
    <code>sem:store()</code> with all arguments omitted, which will access 
    the current database's triple index, using the default rulesets configured 
    for that database.  
	</p>
  </api:param-description><api:param-name>store</api:param-name><api:param-type>sem.store[]</api:param-type></api:param></api:params><api:return>ValueIterator</api:return><api:privilege>
<p xmlns="http://www.w3.org/1999/xhtml"><code>http://marklogic.com/xdmp/privileges/sem-sparql</code></p>
</api:privilege><api:usage>
   <p xmlns="http://www.w3.org/1999/xhtml">The options <code>parse-check</code> and <code>prepare</code> cannot be
   used at the same time.</p>
</api:usage><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
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: &lt;http://www.example.org/dept/108/invoices/&gt;\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"
}

</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
//   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":"&lt;http://www.example.org/dept/108/invoices/paid&gt;",
  "object":"true"
}
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
declareUpdate();
var sem = require("/MarkLogic/semantics.xqy");

// load sample data triples and ontology triple
var string ='\
@prefix rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; .\
@prefix p0: &lt;http://www.example.org/products/&gt; .\
@prefix p2: &lt;http://www.example.com/&gt; .\
\
p2:Henley       &lt;http://www.w3.org/2000/01/rdf-schema#subClassOf&gt;\
                                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: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;\
    prefix rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;\
    prefix p0: &lt;http://www.example.org/products/&gt;\
    prefix p2: &lt;http://www.example.com/&gt;\
 \
    SELECT ?product\
    FROM &lt;graph-1&gt;\
    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"
}</pre></api:example></api:function></api:function-page>