2.3 Matching RDF Literals

The data below contains three RDF literals:

@prefix dt:   <http://example.org/datatype#> .
@prefix ns:   <http://example.org/ns#> .
@prefix :     <http://example.org/ns#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .

:w   ns:p     "cat"@en .
:x   ns:p     "cat"@en-GB .
:y   ns:p     "42"^^xsd:integer .
:z   ns:p     "abc"^^dt:specialDatatype .

Note that, in Turtle, "cat"@en is an RDF literal with a lexical form "cat" and a language en; "cat"@en-GB has a language en-GB; "42"^^xsd:integer is a typed literal with the datatype http://www.w3.org/2001/XMLSchema#integer; and "abc"^^dt:specialDatatype is a typed literal with the datatype http://example.org/datatype#specialDatatype.

This RDF data is the data graph for the query examples in sections 2.3.1–2.3.3.

2.3.1 Matching Language Tags

Language tags in SPARQL are expressed using @ and the language tag, as defined in BestCommon Practice 47 [BCP47].

This following query has no solution because "cat" is not the same RDF literal as either "cat"@en or "cat"@en-GB:

SELECT ?v WHERE { ?v ?p "cat" }
   v    

but the query below will find a solution where variable v is bound to :w because the language tag is specified and matches the given data:

SELECT ?v WHERE { ?v ?p "cat"@en }
v
<http://example.org/ns#w>

SPARQL functions, defined is section 11.3 Operator Mapping, allow one two match language ranges:

SELECT ?v WHERE { ?v ?p ?l
                  FILTER LANGMATCHES(LANG(?l), "EN" }
v
<http://example.org/ns#w>
<http://example.org/ns#x>

2.3.2 Matching Numeric Types

Integers in a SPARQL query indicate an RDF typed literal with the datatype xsd:integer. For example: 42 is a shortened form of  "42"^^<http://www.w3.org/2001/XMLSchema#integer>.

The pattern in the following query has a solution with variable v bound to :y.

SELECT ?v WHERE { ?v ?p 42 }
v
<http://example.org/ns#y>

Section 4.1.2 defines SPARQL shortened forms for xsd:float and xsd:double.

2.3.3 Matching Arbitrary Datatypes

The following query has a solution with variable v bound to :z. The query processor does not have to have any understanding of the values in the space of the datatype. Because the lexical form and datatype IRI both match, the literal matches.

SELECT ?v WHERE { ?v ?p "abc"^^<http://example.org/datatype#specialDatatype> }
v
<http://example.org/ns#z>

2.4 Blank Node Labels in Query Results

Query results can contain labeled blank nodes.

Blank node labels are scoped to a result set (as defined in "SPARQL Query Results XML Format") or, for the CONSTRUCT query form, the result graph. Use of the same label within a result set indicates the same blank node.

Data:
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name   "Alice" .
_:b  foaf:name   "Bob" .
Query:
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ?x ?name
WHERE  { ?x foaf:name ?name }
x name
_:c "Alice"
_:d "Bob"

The results above could equally be given with different blank node labels because the labels in the results only indicate whether RDF terms in the solutions are the same or different.

x name
_:r "Alice"
_:s "Bob"

These two results have the same information: the blank nodes used to match the query are different in the two solutions. There need not be any relation between a label _:a in the result set and a blank node in the data graph with the same label.

An application writer should not expect blank node labels in a query to refer to a particular blank node in the data.

2.5 Building RDF Graphs

SPARQL has several query forms. The SELECT query form returns variable bindings. The CONSTRUCT query form returns an RDF graph. The graph is built based on a template which is used to generate RDF triples based on the results of matching the graph pattern of the query.

Data:

@prefix org:    <http://example.com/ns#> .

_:a  org:employeeName   "Alice" .
_:a  org:employeeId     12345 .

_:b  org:employeeName   "Bob" .
_:b  org:employeeId     67890 .

Query:

PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
PREFIX org:    <http://example.com/ns#>

CONSTRUCT { ?x foaf:name ?name }
WHERE  { ?x org:employeeName ?name }

Results:

@prefix org: <http://example.com/ns#> .
      
_:x foaf:name "Alice" .
_:y foaf:name "Bob" .

which can be serialized in RDF/XML as:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    >
  <rdf:Description>
    <foaf:name>Alice</foaf:name>
  </rdf:Description>
  <rdf:Description>
    <foaf:name>Bob</foaf:name>
  </rdf:Description>
</rdf:RDF>

3 RDF Term Constraints (Informative)

Graph pattern matching produces a solution sequence, where each solution has a set of bindings of variables to RDF terms. SPARQL FILTERs restrict solutions to those for which the filter expression evaluates to TRUE.

This section provides an informal introduction to SPARQL FILTERs; their semantics are defined in Section 11. Testing Values. The examples in this section share one input graph:

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 .

3.1 Restricting the Values of Strings

SPARQL FILTER functions like regex can test RDF literals. regex matches only plain literals with no language tag. regex can be used to match the lexical forms of other literals by using the str function.

Query:

PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
SELECT  ?title
WHERE   { ?x dc:title ?title
          FILTER regex(?title, "^SPARQL") 
        }

Query Result:

title
"SPARQL Tutorial"

Regular expression matches may be made case-insensitive with the "i" flag.

Query:

PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
SELECT  ?title
WHERE   { ?x dc:title ?title
          FILTER regex(?title, "web", "i" ) 
        }

Query Result:

title
"The Semantic Web"

The regular expression language is defined by XQuery 1.0 and XPath 2.0 Functions and Operators and is based on XML Schema Regular Expressions.