Name
cx:sparql — Performs SPARQL queries on RDF datasets.
Synopsis
This step performs SPARQL queries.
| Input port | Primary | Sequence | Content types |
|---|---|---|---|
| source | ✔ | ||
| query | text |
| Output port | Primary | Sequence | Content types |
|---|---|---|---|
| result | ✔ |
| Option name | Type | Default value |
|---|---|---|
| content-type | xs:string? | () |
This is an extension step; to use it, your pipeline must include its declaration.
For example, by including the extension library with an import at the top of your
pipeline:
<p:import href="https://exproc.org/library/rdf.xpl"/>Declaration
1 |<p:declare-step xmlns:cx="http://xmlcalabash.com/ns/extensions"
| xmlns:p="http://www.w3.org/ns/xproc"
| type="cx:sparql">
| <p:input port="source" primary="true"/>
5 | <p:input port="query" content-types="text"/>
| <p:output port="result"/>
| <p:option name="content-type" as="xs:string?"/>
|</p:declare-step>Description
The cx:sparql step performs SPARQL queries. The content type of the
result depends on the content-type option:
application/sparql-results+xml for XML (this is the default if
no type is specified),
application/sparql-results+json for JSON, or
application/rdf+thrift for a binary dataset.
Examples
Consider an RDF address book containing hotels and restaurants. A typical entry might look like this:
1 |<rdf:Description rdf:about="https://so.nwalsh.com/knows/where/adlards">
| <rdf:type rdf:resource="http://nwalsh.com/rdf/contacts#Contact"/>
| <rdf:type rdf:resource="http://nwalsh.com/rdf/contacts#Place"/>
| <rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Organization"/>
5 | <foaf:name>Adlard’s</foaf:name>
| <c:associatedName>Adlard’s</c:associatedName>
| <c:category>Places</c:category>
| <p:category>Restaurants</p:category>
| <v:workAdr rdf:parseType="Resource">
10 | <rdf:type rdf:resource="http://nwalsh.com/rdf/vCard#Address"/>
| <v:street-address>79 Upper St. Giles Street</v:street-address>
| <v:locality>Norwich</v:locality>
| <v:region>Norfolk</v:region>
| <v:postal-code>NR2 1AB</v:postal-code>
15 | <v:country-name>GB</v:country-name>
| </v:workAdr>
| <foaf:homepage rdf:resource="http://www.adlards.co.uk/"/>
|</rdf:Description>(Sadly, now closed.)
We might query the address book using SPARQL:
1 |PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>|PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>|PREFIX c: <http://nwalsh.com/rdf/contacts#>|PREFIX v: <http://nwalsh.com/rdf/vCard#>5 |PREFIX p: <http://nwalsh.com/rdf/pim#>||SELECT ?category ?name ?region ?locality|WHERE|{10 |?x c:associatedName ?name .|?x p:category ?category .|?x p:category "Restaurants" .|?x v:workAdr ?addr .|?addr v:country-name "GB" .15 |?addr v:region ?region .|?addr v:locality ?locality .|}
With a pipeline like this one:
1 |<p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
| xmlns:cx="http://xmlcalabash.com/ns/extensions"
| version="3.0">
|<p:import href="https://xmlcalabash.com/ext/library/rdf.xpl"/>
5 |<p:input port="source" primary="true"/>
|<p:output port="result"/>
|
|<cx:sparql>
| <p:with-input port="query" href="../rdf/query.rq"/>
10 |</cx:sparql>
|
|<p:cast-content-type content-type="text/plain"/>
|
|</p:declare-step>The cx:sparql step can produce XML or JSON results, but for a quick
peek, often the simplest thing to do is cast it to text. This produces:
|---------------+-------------------+-------------+---------------------|| category | name | region | locality ||---------------+-------------------+-------------+---------------------|| Restaurants | The French Horn | Berkshire | Sonning on Thames || Restaurants | Adlard’s | Norfolk | Norwich ||---------------+-------------------+-------------+---------------------|