SPARQL Tips and Tricks
There are several less-than-obvious constructs that will help you construct
useful SPARQL queries. The ACUITy team passes on the following lessons-learned.
- If you are doing function access to a Resource, e.g., regex pattern
matching on the URL, you must wrap the variable reference in the "str()"
function.
Example: suppose you have the triple <http://some_namespace#Test>
<rdfs:label> "Test"
Then SELECT ?x ?l WHERE {?x <rdfs:label> ?l . FILTER (regex(?l,"Test"))} will
match but
SELECT ?x ?l WHERE {?x <rdfs:label> ?l . FILTER (regex(?x,"Test"))} will not
match. The required syntax is
SELECT ?x ?l WHERE {?x <rdfs:label> ?l . FILTER (regex(str(?x),"Test"))}
- To get only the direct subclasses of something, e.g., of
apvf:ServerSideScript, use the special predicate:
SELECT distinct ?t WHERE {?t <urn:x-hp-direct-predicate:http_//www.w3.org/2000/01/rdf-schema#subClassOf>
<apvf:ServerSideScript> }
There are also some pit falls to avoid!
- SPARQL appears to be strongly typed in the sense that domains and ranges
must match. For example, suppose that a Vantage had a property called
currentPerson with range xsd:string. Suppose further that there was a property
called hasHomePage with domain Person and range URL. The the query
SELECT ?URL WHERE {${currentVantage} <currentPerson> ?p . ?p <hasHomePage>
?URL}
would never return anything, even though the value of currentPerson might be "http://....#John_Doe"
and there might be a Person instance with URI "http://...#John_Doe".
In other words, they appear to the human eye to match but since one is an
xsd:string and the other is an Individual of type Person, they do not. The
solution is to make sure that the domains and ranges of properties that will
be used in constructing graph patterns are consistent.
Copyright General Electric Company 2006, 2007
Last revised
03/05/2007