Inspired by a few related questions (in particular this one), I wanted to ask a more general form of the question: how can I specify preferences for basic graph pattern matching in SPARQL (1.1) such that potentially many lower preferences are only matched if a higher preference cannot be found?
It seems there are a few potential solutions and I hope to gather some alternative patterns, some of which may work best in different concrete scenarios.
To make the general question concrete, but in a way that can easily be generalised, say I want to get the labels of each resource of type foaf:Person where, in order, I prefer the values from the following predicates:
foaf:name
skos:prefLabel
skos:altLabel
rdfs:label
(potentially more)
The basic query in SPARQL would be:
PREFIX ...
SELECT ?person ?name
WHERE {
?person a foaf:Person ; foaf:name ?name .
}
How can this query be adapted to return names from predicates in the above preference order such that values for a given predicate are only returned if no value is returned for a higher-preference predicate? (I.e., if we find a foaf:name that suffices, else if we find a skos:prefLabel that suffices, else ...).