SPARQL: sorting resources by label?

The closest I get is the following SPARQL query:

> SELECT DISTINCT ?subj ?label
> WHERE {
>     GRAPH ?graph {
>         ?subj ?pred ?obj .
>         OPTIONAL {
>             ?subj ?labelPred ?label .
>             FILTER (
>                 (?labelPred = <http://www.w3.org/2000/01/rdf-schema#label>) # (1)
>             )
>             FILTER( isLiteral(?label) )
>         }
>     }
>     FILTER (?graph = <http://hypergraphs.de/TestGraph>)
> }
> ORDER BY ?label ?subj

Comments:

- This solution also works for multiple label predicates (i.e., if there are subproperties of rdfs:label), then the unary disjunction (1) has more components.

- ?graph is necessary, because Sesame does not support datasets and I want to restrict the query to all graphs that are currently "visible".

- This query returns unlabeled resources first (?label is unbound), then labeled resources. Better would be to show labeled resources first. Best would be to mix them, where unlabeled resources are sorted according to their qname.

Can this be improved?

Thanks for any comments or suggestions...

Axel

-- 
Axel.Rauschmayer@ifi.lmu.de
http://www.pst.ifi.lmu.de/~rauschma/

Received on Saturday, 13 March 2010 03:17:20 UTC