charles okojie
charles okojie

Reputation: 728

SPARQL query result not showing anything from graph

I am trying to run a sparql query on this dataset below

@prefix ex: <http://www.example.com/ont#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

{
    ex:repository ex:createdBy ex:repOwner; ex:title "Rep_1".
}

ex:books 
{
    ex:book_1 a ex:Science; ex:size "100"; ex:title "Science book 1".
    ex:book_2 a ex:Science; ex:size "1000"; ex:title "Science book 2". 
    ex:book_3 a ex:Fantasy; ex:size "100"; ex:title "Fantasy book 1".
}

When i try to query with sparql i get an empty table result. I dont get any result from the graph. Below is my query

 prefix ex: <http://www.example.com/ont#> 
 prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
 prefix xsd: <http://www.w3.org/2001/XMLSchema#> 


SELECT * WHERE
{
  GRAPH ex:books 
  {
    GRAPH ?g { }
    {
      ?s ?p ?o;
    }
  }
}

Its in trig notation. Any help?

Upvotes: 2

Views: 365

Answers (1)

RobV
RobV

Reputation: 28646

Are you sure you've loading your data and are making your query correctly? Without a complete minimal example i.e. the code you use to load and query the data (or relevant steps used if you are doing this with a command line/GUI tool) we can't really do much more than speculate.

When I load your data into Apache Jena Fuseki (disclaimer - I'm a committer on the Apache Jena project) the query runs successfully and produces the following result:

------------------------------------------------------
| g        | s         | p        | o                |
======================================================
| ex:books | ex:book_1 | ex:title | "Science book 1" |
| ex:books | ex:book_1 | ex:size  | "100"            |
| ex:books | ex:book_1 | rdf:type | ex:Science       |
| ex:books | ex:book_3 | ex:title | "Fantasy book 1" |
| ex:books | ex:book_3 | ex:size  | "100"            |
| ex:books | ex:book_3 | rdf:type | ex:Fantasy       |
| ex:books | ex:book_2 | ex:title | "Science book 2" |
| ex:books | ex:book_2 | ex:size  | "1000"           |
| ex:books | ex:book_2 | rdf:type | ex:Science       |
------------------------------------------------------

Your Query

One odd thing about your query is that you have a GRAPH ?g {} clause which is an empty scan over all graphs which should produce the names of all graphs (assuming a well behaved standard compliant SPARQL implementation). This is then joined to a scan of ?s ?p ?o over the ex:books graph.

This means that you will be combining every possible graph name with every possible match in ex:books so the ?g variable will not be telling you which graph the data came from (unless you only have a single named graph) which I assume was your actual intention?

Try the following:

PREFIX ex: <http://www.example.com/ont#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 

SELECT * WHERE
{
  GRAPH ?g
  {
    ?s ?p ?o;
  }
}

This finds all matches in all graphs which is perhaps not what you want either. If you did only want to query the specific ex:books graph try this instead:

PREFIX ex: <http://www.example.com/ont#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 

SELECT * WHERE
{
  GRAPH ex:books
  {
    ?s ?p ?o;
  }
}

And if you really need the value of ex:books in a variable for later processing you could then introduce it as a constant using BIND e.g.

PREFIX ex: <http://www.example.com/ont#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 

SELECT * WHERE
{
  GRAPH ex:books
  {
    ?s ?p ?o;
    BIND(ex:books AS ?g)
  }
}

Debugging Tips

The key thing to do to debug is to try running a query that should return all the data e.g.

SELECT * WHERE
{
  { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } }
}

If this returns nothing then you know you have loaded your data incorrectly or are not querying the data correctly. If it does return something it should tell you exactly where and what your data is.

Often then you may see that you've made an inadvertent typo somewhere which can often be the cause of queries not returning if the URIs in the loaded data and the query don't match.

Upvotes: 1

Related Questions