问题
I'm new to the semantic web and I'm trying to figure out how to write a SPARQL query to extract from dbpedia everything about a particular subject. Not just it's proprieties, but also everything related to it.
I'm not even sure how to start such a query.
I would like to get all triples about the World Wide Web.
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT DISTINCT ?s ?p ?o
WHERE {
?s ?p ?o .
?s ?p dbpedia:World_Wide_Web
# FILTER( lang(?s) = "en" ) -- doesn't work with filter
}Limit 100
This only returns some triples with a owl:sameAs predicate
Can you help me?
回答1:
You can get all the triples that have dbpedia:World_Wide_Web
as their subject or object with a query like this (this only gives 1000, of course). For any objects that are literals, we can restrict their language value:
select ?s ?p ?o where {
values ?web { dbpedia:World_Wide_Web }
{ ?web ?p ?o bind( ?web as ?s ) } union
{ ?s ?p ?web bind( ?web as ?o ) }
# for literal objects, take only English ones
filter( !isLiteral(?o) || langMatches(lang(?o),'en') )
}
limit 1000
SPARQL results
This includes results like the following, which seems to align with what you mentioned in the comments:
http://dbpedia.org/resource/World_Wide_Web http://dbpedia.org/property/company http://dbpedia.org/resource/CERN
http://dbpedia.org/resource/World_Wide_Web http://dbpedia.org/property/inventor http://dbpedia.org/resource/Tim_Berners-Lee
That will produce a lot of results, so you might want to restrict the properties that you can use. You should be able to do the following, but on the current DBpedia endpoint, it causes an error.
select ?s ?p ?o where {
values ?web { dbpedia:World_Wide_Web }
values ?p { rdf:type dbpedia-owl:abstract }
{ ?web ?p ?o bind( ?web as ?s ) } union
{ ?s ?p ?web bind( ?web as ?o ) }
}
limit 1000
Virtuoso 37000 Error SP031: SPARQL compiler: Internal error: sparp_gp_attach_filter_cbk(): attempt to attach a filter with used variable
SPARQL query:
define sql:big-data-const 0
#output-format:text/html
define sql:signal-void-variables 1 define input:default-graph-uri <http://dbpedia.org> select ?s ?p ?o where {
values ?web { dbpedia:World_Wide_Web }
values ?p { rdf:type dbpedia-owl:abstract }
{ ?web ?p ?o bind( ?web as ?s ) } union
{ ?s ?p ?web bind( ?web as ?o ) }
}
limit 1000
Instead, as a workaround, you can do this:
select ?s ?p ?o where {
values ?web { dbpedia:World_Wide_Web }
{ ?web ?p ?o bind( ?web as ?s ) }
union
{ ?s ?p ?web bind( ?web as ?o ) }
filter( ?p in (rdf:type, dbpedia-owl:abstract )) ###
}
limit 1000
SPARQL results
来源:https://stackoverflow.com/questions/21002440/sparql-about-dbpediaworld-wide-web