javasparqljenadbpediadublin-core

SPARQL query against Portuguese dbpedia endpoint for dcterms:subject returns no results


I tested that query:

SELECT ?comment WHERE {<http://pt.dbpedia.org/resource/Portugal> dcterms:subject ?comment}

at http://pt.dbpedia.org/sparql and I get the correct result:

http://pt.dbpedia.org/resource/Categoria:Portugal

But I'm using Jena and when I try to do that query with Jena I get no results. That's the way I'm doing the query with Jena:

private String getComment(String uri) {
          RDFNode node;
          String comment = "";

            final String QUERY = 
                    "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                    "PREFIX dcterms: <http://purl.org/dc/terms/subject>\n" +
                    "SELECT ?comment WHERE {" +
                    "<" + uri + "> dcterms:subject ?comment." +
                    "}";

      final String ENDPOINT = "http://pt.dbpedia.org/sparql";
      final ResultSet rs = QueryExecutionFactory.sparqlService( ENDPOINT, QUERY ).execSelect();

                while( rs.hasNext() ) {
                       QuerySolution querySolution = rs.next();
                       node = querySolution.get("comment");
                       comment = node.toString();
                    }

                return comment;
      }

Is there anything wrong? Thank you!


Solution

  • The dcterms: prefix has a typo

    The dcterms: prefix is incorrect (it has subject at the end). It should be

    http://purl.org/dc/terms/
    

    Use ParameterizedSparqlStrings to avoid injection problems

    Also, the way that you're splicing the uri parameter into the query is a bit brittle, and it's subject to injection attacks. E.g., what would happen if uri were the following string?

    > <>* <> . <http://example.org/secretData> ?anyProperty ?comment . #
    

    You'd leak information about http://example.org/secretData, since <> <>* <> will always match, and then you'd bind ?comment to all the values of any property of http://example.org/secretData. There's an example of how to do this in this answer to get latitude and longitude of a place dbpedia.