简体   繁体   中英

How to filter query results by url prefix in SPARQL?

This is the query I have,

PREFIX dbr: <http://dbpedia.org/resource/>

SELECT ?p ?x
WHERE
{ 
  dbr:Australia ?p ?x 
}

I need only URI in the results. x and p should be in the format of http://dbpedia.org/resource/something and http://dbpedia.org/property/something . Please help me to go through this.

This will get what you're asking for --

PREFIX dbr: <http://dbpedia.org/resource/>

SELECT ?p ?x
WHERE
{ 
  dbr:Australia ?p ?x 
  FILTER ( STRSTARTS ( STR ( ?p ), "http://dbpedia.org/property/" ) )
  FILTER ( STRSTARTS ( STR ( ?x ), "http://dbpedia.org/resource/" ) )
}
ORDER BY ?p ?x

The results of the following might be better, though it doesn't restrict all URIs to the two prefixes you've wished for. Note that the next update of the DBpedia dataset (partially visible now on the DBpedia-Live endpoint ) will change much of the data, and http://dbpedia.org/property/ will no longer be the prefix for most of Australia's attributes/predicates --

PREFIX dbr: <http://dbpedia.org/resource/>

SELECT ?p ?x
WHERE
{ 
  dbr:Australia ?p ?x 
  FILTER ( ISURI ( ?p ) )
  FILTER ( ISURI ( ?x ) )
}
ORDER BY ?p ?x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM