10.2 VALUES: Providing inline data
Data can be directly written in a graph pattern or added to a query using VALUES. VALUES provides inline data as a solution sequence which are combined with the results of query evaluation by a join operation. It can be used by an application to provide specific requirements on query
results and also by SPARQL query engine implementations that provide federated query through the SERVICE keyword to send a more constrained query to a remote query service.
10.2.1 VALUES syntax
VALUES allows multiple variables to be specified in the data block; there is a special syntax
for the common case of specifying just one variable and some values.
In the following example, there is a table of two variables, ?x and ?y. The second row has no value for ?y.
VALUES (?x ?y) {
(:uri1 1)
(:uri2 UNDEF)
}
Optionally, when there is a single variable and some values:
VALUES ?z { "abc" "def" }
which is the same as using the general form:
VALUES (?z) { ("abc") ("def") }
10.2.2 VALUES Examples
A VALUES block of data can appear in a query pattern or at the end of a SELECT query, including a subquery.
Data:
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix : <http://example.org/book/> .
@prefix ns: <http://example.org/ns#> .
:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .
Query:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://example.org/book/>
PREFIX ns: <http://example.org/ns#>
SELECT ?book ?title ?price
{
VALUES ?book { :book1 :book3 }
?book dc:title ?title ;
ns:price ?price .
}
Result:
| book | title | price |
|---|---|---|
| <http://example.org/book/book1> | "SPARQL Tutorial" | 42 |
If a variable has no value for a particular solution in the VALUES clause, the keyword UNDEF is used instead of an RDF term.
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://example.org/book/>
PREFIX ns: <http://example.org/ns#>
SELECT ?book ?title ?price
{
?book dc:title ?title ;
ns:price ?price .
VALUES (?book ?title)
{ (UNDEF "SPARQL Tutorial")
(:book2 UNDEF)
}
}
| book | title | price |
|---|---|---|
| <http://example.org/book/book1> | "SPARQL Tutorial" | 42 |
| <http://example.org/book/book2> | "The Semantic Web" | 23 |
In this example, the VALUES might have been specified to execute over the results of the SELECT query:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://example.org/book/>
PREFIX ns: <http://example.org/ns#>
SELECT ?book ?title ?price
{
?book dc:title ?title ;
ns:price ?price .
}
VALUES (?book ?title)
{ (UNDEF "SPARQL Tutorial")
(:book2 UNDEF)
}
This is a different query but, in the example situation, has the same results.
Copyright © 2013 W3C® (MIT, ERCIM, Keio, Beihang). This software or document includes material copied from or derived from SPARQL 1.1 Query.