Functional Forms
bound
xsd:boolean BOUND (variable var)
Returns true if var is bound to a value. Returns false otherwise. Variables with the value NaN or INF
are considered bound.
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
_:a foaf:givenName "Alice".
_:b foaf:givenName "Bob" .
_:b dc:date "2005-04-04T04:04:04Z"^^xsd:dateTime .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?givenName
WHERE { ?x foaf:givenName ?givenName .
OPTIONAL { ?x dc:date ?date } .
FILTER ( bound(?date) ) }
Query result:
| givenName |
|---|
| "Bob" |
One may test that a graph pattern is not expressed by specifying an OPTIONALgraph pattern that introduces a variable and testing to see that the variable is notbound. This is called Negation as Failure in logic programming.
This query matches the people with a name but no expressed date:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?name
WHERE { ?x foaf:givenName ?name .
OPTIONAL { ?x dc:date ?date } .
FILTER (!bound(?date)) }
Query result:
| name |
|---|
| "Alice" |
Because Bob's dc:date was known, "Bob" was not a solution to the query.
IF
rdfTerm IF (expression1, expression2, expression3)
The IF function form evaluates the first argument, interprets it as a effective boolean value, then returns the value of expression2 if the EBV is true, otherwise it returns the value of expression3. Only one of expression2 and expression3 is evaluated. If evaluating the first argument raises an error, then an error is
raised for the evaluation of the IF expression.
Examples: Suppose ?x = 2, ?z = 0 and ?y is not bound in some query solution:
IF(?x = 2, "yes", "no") |
returns "yes" |
IF(bound(?y), "yes", "no") |
returns "no" |
IF(?x=2, "yes", 1/?z) |
returns "yes", the expression 1/?z is not evaluated |
IF(?x=1, "yes", 1/?z) |
raises an error |
IF("2" > 1, "yes", "no") |
raises an error |
COALESCE
rdfTerm COALESCE(expression, ....)
The COALESCE function form returns the RDF term value of the first expression that evaluates without
error. In SPARQL, evaluating an unbound variable raises an error.
If none of the arguments evaluates to an RDF term, an error is raised. If no expressions are evaluated without error, an error is raised.
Examples: Suppose ?x = 2 and ?y is not bound in some query solution:
COALESCE(?x, 1/0) |
returns 2, the value of x |
COALESCE(1/0, ?x) |
returns 2 |
COALESCE(5, ?x) |
returns 5 |
COALESCE(?y, 3) |
returns 3 |
COALESCE(?y) |
raises an error because y is not bound. |
NOT EXISTS and EXISTS
There is a filter operator EXISTS that takes a graph pattern. EXISTS returns true/false depending on whether the pattern matches the dataset given the bindings in the current
group graph pattern, the dataset and the active graph at this point in the query evaluation. No additional binding of variables occurs.
The NOT EXISTS form translates into fn:not(EXISTS{...}).
xsd:boolean NOT EXISTS { pattern }
Returns false if pattern matches. Returns true otherwise.
NOT EXISTS { pattern } is equivalent to fn:not(EXISTS { pattern }).
xsd:boolean EXISTS { pattern }
Returns true if pattern matches. Returns false otherwise.
Variables in the pattern that are bound in the current solution mapping take the value that they have from the solution mapping. Variables in the pattern
pattern that are not bound in the current solution mapping take part in pattern matching.
To facilitate this, we introduce a function Exists that evaluates a SPARQL Algebra expression and returns true or false, depending on whether there are any solutions to the pattern, given the solution mapping being tested by the filter operation.
logical-or
xsd:boolean xsd:boolean left || xsd:boolean right
Returns a logical OR of left and right. Note that logical-or operates on the effective boolean value of its arguments.
Note: see section 17.2, Filter Evaluation, for the || operator's treatment of errors.
logical-and
xsd:boolean xsd:boolean left && xsd:boolean right
Returns a logical AND of left and right. Note that logical-and operates on the effective boolean value of its arguments.
Note: see section 17.2, Filter Evaluation, for the && operator's treatment of errors.
RDFterm-equal
xsd:boolean RDF term term1 = RDF term term2
Returns TRUE if term1 and term2 are the same RDF term as defined in Resource Description Framework (RDF): Concepts and Abstract Syntax [Resource Description Framework (RDF): Concepts and Abstract Syntax]; produces a type error if the arguments are both literal but are not the same RDF
term *; returns FALSE otherwise. term1 and term2 are the same if any of the following is true:
- term1 and term2 are equivalent IRIs as defined in 6.4 RDF URI References of [Resource Description Framework (RDF): Concepts and Abstract Syntax].
- term1 and term2 are equivalent literals as defined in 6.5.1 Literal Equality of [Resource Description Framework (RDF): Concepts and Abstract Syntax].
- term1 and term2 are the same blank node as described in 6.6 Blank Nodes of [Resource Description Framework (RDF): Concepts and Abstract Syntax].
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice".
_:a foaf:mbox <mailto:alice@work.example> .
_:b foaf:name "Ms A.".
_:b foaf:mbox <mailto:alice@work.example> .
This query finds the people who have multiple foaf:name triples:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name1 ?name2
WHERE { ?x foaf:name ?name1 ;
foaf:mbox ?mbox1 .
?y foaf:name ?name2 ;
foaf:mbox ?mbox2 .
FILTER (?mbox1 = ?mbox2 && ?name1 != ?name2)
}
Query result:
| name1 | name2 |
|---|---|
| "Alice" | "Ms A." |
| "Ms A." | "Alice" |
In this query for documents that were annotated at a specific date and time (New Year's Day 2005, measures in timezone +00:00), the RDF terms are not the same, but have equivalent values:
@prefix a: <http://www.w3.org/2000/10/annotation-ns#> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
_:b a:annotates <http://www.w3.org/TR/rdf-sparql-query/> .
_:b dc:date "2004-12-31T19:00:00-05:00"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
PREFIX a: <http://www.w3.org/2000/10/annotation-ns#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?annotates
WHERE { ?annot a:annotates ?annotates .
?annot dc:date ?date .
FILTER ( ?date = xsd:dateTime("2005-01-01T00:00:00Z") )
}
| annotates |
|---|
| <http://www.w3.org/TR/rdf-sparql-query/> |
- Invoking RDFterm-equal on two typed literals tests for equivalent values. An extended
implementation may have support for additional datatypes. An implementation processing
a query that tests for equivalence on unsupported datatypes (and non-identical lexical
form and datatype IRI) returns an error, indicating that it was unable to determine
whether or not the values are equivalent. For example, an unextended implementation
will produce an error when testing either
"iiii"^^my:romanNumeral = "iv"^^my:romanNumeralor"iiii"^^my:romanNumeral != "iv"^^my:romanNumeral.
sameTerm
xsd:boolean sameTerm (RDF term term1, RDF term term2)
Returns TRUE if term1 and term2 are the same RDF term as defined in Resource Description Framework (RDF): Concepts and Abstract Syntax [Resource Description Framework (RDF): Concepts and Abstract Syntax]; returns FALSE otherwise.
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice".
_:a foaf:mbox <mailto:alice@work.example> .
_:b foaf:name "Ms A.".
_:b foaf:mbox <mailto:alice@work.example> .
This query finds the people who have multiple foaf:name triples:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name1 ?name2
WHERE { ?x foaf:name ?name1 ;
foaf:mbox ?mbox1 .
?y foaf:name ?name2 ;
foaf:mbox ?mbox2 .
FILTER (sameTerm(?mbox1, ?mbox2) && !sameTerm(?name1, ?name2))
}
Query result:
| name1 | name2 |
|---|---|
| "Alice" | "Ms A." |
| "Ms A." | "Alice" |
Unlike RDFterm-equal , sameTerm can be used to test for non-equivalent typed literals with unsupported datatypes:
@prefix : <http://example.org/WMterms#> .
@prefix t: <http://example.org/types#> .
_:c1 :label "Container 1" .
_:c1 :weight "100"^^t:kilos .
_:c1 :displacement "100"^^t:liters .
_:c2 :label "Container 2" .
_:c2 :weight "100"^^t:kilos .
_:c2 :displacement "85"^^t:liters .
_:c3 :label "Container 3" .
_:c3 :weight "85"^^t:kilos .
_:c3 :displacement "85"^^t:liters .
PREFIX : <http://example.org/WMterms#>
PREFIX t: <http://example.org/types#>
SELECT ?aLabel1 ?bLabel
WHERE { ?a :label ?aLabel .
?a :weight ?aWeight .
?a :displacement ?aDisp .
?b :label ?bLabel .
?b :weight ?bWeight .
?b :displacement ?bDisp .
FILTER ( sameTerm(?aWeight, ?bWeight) && !sameTerm(?aDisp, ?bDisp)) }
| aLabel | bLabel |
|---|---|
| "Container 1" | "Container 2" |
| "Container 2" | "Container 1" |
The test for boxes with the same weight may also be done with the '=' operator (RDFterm-equal) as the test for "100"^^t:kilos = "85"^^t:kilos will result in an error, eliminating that potential solution.
IN
boolean rdfTerm IN (expression, ...)
The IN operator tests whether the RDF term on the left-hand side is found in the values
of list of expressions on the right-hand side. The test is done with "=" operator,
which tests for the same value, as determined by the operator mapping.
A list of zero terms on the right-hand side is legal.
Errors in comparisons cause the IN expression to raise an error if the RDF term being tested is not found elsewhere
in the list of terms.
The IN operator is equivalent to the SPARQL expression:
(lhs = expression1) || (lhs = expression2) || ...
Examples:
2 IN (1, 2, 3) |
true |
2 IN () |
false |
2 IN (<http://example/iri>, "str", 2.0) |
true |
2 IN (1/0, 2) |
true |
2 IN (2, 1/0) |
true |
2 IN (3, 1/0) |
raises an error |
17.4.1.10 NOT IN
boolean rdfTerm NOT IN (expression, ...)
The NOT IN operator tests whether the RDF term on the left-hand side is not found in the values
of list of expressions on the right-hand side. The test is done with "!=" operator,
which tests for not the same value, as determined by the operator mapping.
A list of zero terms on the right-hand side is legal.
Errors in comparisons cause the NOT IN expression to raise an error if the RDF term being tested is not found to be in the
list elsewhere in the list of terms.
The NOT IN operator is equivalent to the SPARQL expression:
(lhs != expression1) && (lhs != expression2) && ...
NOT IN (...) is equivalent to !(IN (...)).
Examples:
2 NOT IN (1, 2, 3) |
false |
2 NOT IN () |
true |
2 NOT IN (<http://example/iri>, "str", 2.0) |
false |
2 NOT IN (1/0, 2) |
false |
2 NOT IN (2, 1/0) |
false |
2 NOT IN (3, 1/0) |
raises an error |
Copyright © 2013 W3C® (MIT, ERCIM, Keio, Beihang). This software or document includes material copied from or derived from SPARQL 1.1 Query.