SPARQL Query final label [duplicate] - sparql

This question already has an answer here:
Why does my SPARQL query return the URI of a resource instead of its name?
(1 answer)
Closed 6 years ago.
I have a SPARQL Query that returns the Europe capitals and their population. The query looks like this:
select ?s ?pop
where {
?s <http://dbpedia.org/ontology/populationTotal> ?pop .
?s a <http://dbpedia.org/ontology/place>
}
In this state, it returns the names of the cities in the following form: e.g. "<http://dbpedia.org/resource/London>" and what I want is to display only London in this case. So, is there a way I can tell SPARQL that I want only the final label?
I am querying against this endpoint: https://rdf.s4.ontotext.com/4730361296/demo01/repositories/test01

The advice here is similar to other questions - use SPARQL to inspect the data. So first try this query to see if there are any label properties defined:
select *
where {
?s <http://dbpedia.org/ontology/populationTotal> ?pop .
?s a <http://dbpedia.org/ontology/place> .
?s ?p ?o .
}
In this case you'll find that no label properties have been defined for place class definitions. If desired you can take the local name - the text after the last slash (or hash) as the name. Try this query:
select *
where {
?s <http://dbpedia.org/ontology/populationTotal> ?pop .
?s a <http://dbpedia.org/ontology/place> .
BIND(REPLACE(xsd:string(?s), ".*[/#]", "") AS ?label)
}

Related

How do I recursively follow blank nodes to get all triples about an object? [duplicate]

This question already has answers here:
How do I construct get the whole sub graph from a given resource in RDF Graph?
(3 answers)
Closed 10 months ago.
Here is a data object for a user with ID 123, that is age 50 and likes spaghetti.
[ a :user;
:user_id 123;
:demographics [ :age 50 ];
:favorite_food [ :italian [ :spaghetti true ]]
].
Is there any sparql query where I could get back all the triples related to this user without having to do a UNION for each possible blank node value follow?
I'm trying to convert json into triples, then turn the triples back into json,
so it would be very convenient be able to query of objects of unknown nesting depth without adding a parent ID property to every child element of the json.
This seems to work but I would have to do a UNION for every layer of nesting
select ?e1 ?a1 ?v1 { ?e :user_id 123.
{ ?e ?a1 ?v1. BIND(?e AS ?e1) }
UNION { ?e ?a ?v. ?v ?a1 ?v1. BIND(?v AS ?e1). } }
UninformedUser has a pointed out a solution. From the look of the query it seems like the recursive property path trick wouldn't grab the first level before nesting, but it does grab all data.
select ?s ?p ?o { ?e :user_id 123. ?e (<>|!<>)* ?s. ?s ?p ?o }
#UninformedUser provided a solution in a comment. Though the OP thought this query using the recursive property path trick wouldn't grab the first level before nesting, it does grab all their desired data.
SELECT ?s ?p ?o
{ ?e :user_id 123 .
?e (<>|!<>)* ?s .
?s ?p ?o
}

SPARQL Query to Identify Predicates from One of Many Graphs [duplicate]

This question already has an answer here:
SPARQL - Restricting Result Resource to Certain Namespace(s)
(1 answer)
Closed 4 years ago.
I have created an ontology by grouping together many ontologies.
I want to use SPARQL to identify all middle terms (relationships) from one ontology from the group ontology.
The following approach only produces a pyparsing error.
g = rdflib.Graph()
result = g.parse("without-bfo.owl")
qres = g.query(
""" PREFIX sudo: <http://purl.url/sudo/ontology#>
SELECT ?v
WHERE {
?s sudo:?v ?o.
}""")
If I remove the sudo: prefix, this query returns all triples.
You can check if the relation starts with your namespace with CONTAINS
SELECT ?v
WHERE {
?s ?v ?o.
FILTER CONTAINS(?v, "http://purl.url/sudo/ontology#")
}
You can also try STRSTARTS
see w3 documentation
#Arcturus was close.
The following worked for me. One has to declare ?v as a string using STR. This SO post suggested the syntax.
qres = g.query(
""" SELECT DISTINCT ?v
WHERE {
?s ?v ?o .
FILTER CONTAINS(STR(?v), "sudo")
}""")
for row in qres:
print row

How to return all S->P->O triples from a starting resource to a specified path depth?

My goal is to graphically represent the S->P->O relations within a depth two edges from the specified resource, p:Person_1. I want all relations within that path length to be returned from my query as ?s, ?p, ?o for further processing in my graphical application.
I tried the first query below which gives me my first set of ?s ?p ?o with repeats, then ?p2, ?o2, ?p3, ?o3 as additional columns in the result. I want to bind ?p2 and ?p3 to ?p, ?o2 and ?o3 to ?o.
SELECT *
WHERE {
p:Person_1 ?p ?o .
BIND("p:Person_1" as ?s)
OPTIONAL{
?o ?p2 ?o2 .
}
OPTIONAL{
?o2 ?p3 ?o3 .
}
}
Then, based on How do I construct get the whole sub graph from a given resource in RDF Graph?, I tried using CONSTRUCT to return the graph.
PREFIX p: <http://www.example.org/person/>
PREFIX x: <example.org/foo/>
construct { ?s ?p ?o }
FROM <http://localhost:8890/MYGRAPH>
where { p:Person_1 (x:|!x:)* ?s .
?s ?p ?o .
}
I am using Virtuoso and I get the error:
Virtuoso 37000 Error SP031: SPARQL compiler: Variable ?_::trans_subj_9_3 in T_IN list is not a value from some triple
I could post-process the result from my first query but I want to learn how to do this correctly with SPARQL, preferably on Virtuoso.
Update after testing the advice from #AKSW :
Both CONSTRUCT and SELECT statements work with the pattern suggested.
CONSTRUCT { ?s ?p ?o }
FROM <http://localhost:8890/MYGRAPH>
where { p:Person_1 (x:foo|!x:bar)* ?s .
?s ?p ?o .
} LIMIT 100
and:
SELECT s ?p ?o
FROM <http://localhost:8890/MYGRAPH>
where { p:Person_1 (x:foo|!x:bar)* ?s .
?s ?p ?o .
} LIMIT 100
The SELECT results in several duplicates that cannot be removed using DISTINCT, which results in an error that I assume is due to the 'datatype' of some of the returned values.
Virtuoso 22023 Error SR066: Unsupported case in CONVERT (DATETIME -> IRI_ID)
It appears some post-SPARQL processing is in order.
This gets me most of the way there. Still hoping I can find a solution for SPARQL that is like Cypher's "number of hops away" :
OPTIONAL MATCH path=s-[*1..3]-(o)
Here is a SPARQL query that works in Virtuoso. Note the SPARQL W3C standard does not support this syntax and it will fail in other triplestores.
PREFIX p: <http://www.example.org/person/>
PREFIX x: <example.org/foo/>
# CONSTRUCT {?s ?p ?o} # If you wish to return the graph
SELECT ?s ?p ?o # To return the triples
FROM <http://localhost:8890/MYGRAPH>
where { p:Person_1 (x:foo|!x:bar){1,3} ?s .
?s ?p ?o .
}LIMIT 100
See also K. Idehen's wiki entry here: http://linkedwiki.com/exampleView.php?ex_id=141
And thanks to #Joshua Taylor for advice in the same area.
Working Drafts of SPARQL 1.1 Property Paths included the {n,m} operator for handling this issue, which was implemented (and will remain supported) in Virtuoso. Here's a tweak to #tim's response.
Live SPARQL Query Results Page using the DBpedia endpoint (which is a Virtuoso instance).
Live SPARQL Query Definition Page that opens up query source code in the default DBpedia query editor.
Actual Query Example:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?s AS ?Entity
?o AS ?Category
WHERE {
?s rdf:type <http://dbpedia.org/ontology/AcademicJournal> ;
rdf:type{1,3} ?o
}
LIMIT 100
Should you be looking for LinkedIn-like presentation of Contact Networks and Degrees of Separation between individuals, here is an example using Virtuoso-specific SPARQL Extensions that solve this particular issue:
SELECT ?o AS ?WebID
((SELECT COUNT (*) WHERE {?o foaf:knows ?xx})) AS ?contact_network_size
?dist AS ?DegreeOfSeparation
<http://www.w3.org/People/Berners-Lee/card#i> AS ?knowee
WHERE
{
{
SELECT ?s ?o
WHERE
{
?s foaf:knows ?o
}
} OPTION (TRANSITIVE, t_distinct, t_in(?s), t_out(?o), t_min (1), t_max (4), t_step ('step_no') AS ?dist) .
FILTER (?s= <http://www.w3.org/People/Berners-Lee/card#i>)
FILTER (isIRI(?s) and isIRI(?o))
}
ORDER BY ?dist DESC (?contact_network_size)
LIMIT 500
Note: this approach is the only way (at the current time) to expose actual relational hops between entities in an Entity Relationship Graph that includes Transitive relations.
Live Link to Query Results
Live Link to Query Source Code
Bearing in mind that the r{n,m} operator was deprecated in the final SPARQL 1.1 (but will remain supported in Virtuoso), you can use r/r?/r? instead of r{1,3}, if you want to work strictly off the current spec:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?s AS ?Entity
?o AS ?Category
WHERE {
?s rdf:type <http://dbpedia.org/ontology/AcademicJournal> ;
rdf:type / rdf:type? / rdf:type? ?o
}
LIMIT 100
Here's a live example, against the DBpedia instance hosted in Virtuoso.

Select All That Has The Same Prefix SPARQL [duplicate]

This question already has an answer here:
How to check prefix of variables in SPARQL?
(1 answer)
Closed 7 years ago.
I would like to return all triples where all subjects have the same prefix.
PREFIX dv: <http://example.org/example_vocabulary:>
SELECT DISTINCT *
FROM <http://example.org/dataset.example>
WHERE {
?s ?p ?o .
}
You should treat the URI as a string and basically filter your variable based on what you need. Since you are looking for a prefix, you can use strstarts. For example, something along these lines will work:
PREFIX dv: <http://example.org/example_vocabulary>
SELECT DISTINCT *
FROM <http://example.org/dataset.example>
WHERE {
?s ?p ?o .
filter strstarts(str(?s),str(dv:))
}
You should read up on string function.

How do I restrict my search in dbpedia using SPARQL to "Persons" only

I am using a query to output the influences of all people listed in wikipedia (where possible). I am using http://dbpedia.org/snorql/. My code so far is:
SELECT *
WHERE {
?p a
<http://dbpedia.org/ontology/Person> .
?p <http://dbpedia.org/ontology/influenced> ?influenced.
}
The problem is that the influenced output includes things like genres and political ideologies. I want to restrict it to only output "people" and "people who were influenced by those people". Thanks in advanced.
Try:
SELECT *
WHERE {
?p a
<http://dbpedia.org/ontology/Person> .
?p <dbpedia-owl:birthYear> ?birthYear.
?p <http://dbpedia.org/ontology/influenced> ?influenced.
?influenced a <http://dbpedia.org/ontology/Person>.
}
EDITED TO ADD BIRTH YEAR