Question

I use AllegroGraph and Sparql 1.1.

I need to do ascending sort on a column and make the Sparql query to return empty values at the last.

Sample data:

<http://mydomain.com/person1> <http://mydomain.com/name> "John"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://mydomain.com/person>

<http://mydomain.com/person2> <http://mydomain.com/name> "Abraham"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://mydomain.com/person>

<http://mydomain.com/person3> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://mydomain.com/person>

Here I need the Sparql to return Abraham, followed by John and person3 that does not have a name attribute.

Query I use:

select ?name ?person {
  ?person <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://mydomain.com/person>.
  optional {?person  <http://mydomain.com/name> ?name.}
  } order by asc(?name )

Current output is person3 (null), followed by Abraham and John. enter image description here Please let me know your thoughts.

Was it helpful?

Solution

I don't have AllegroGraph at hand but AFAIK it supports multiple order conditions:

select ?name ?person {
  ?person <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://mydomain.com/person> .
  optional {?person  <http://mydomain.com/name> ?name . }
} order by (!bound(?name)) asc(str(?name))

First condition sorts based on whether ?name is bound or not and if this condition does not find a difference, the second condition is used. Note the use of str() to convert rdf:XMLLiteral to a datatype for which comparison is supported.

(You may also want to add . at the end of each row in your ntriples data.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top