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
Related
I guess I am stuck at the basics with SPARQL. Can someone help ?
I simply wnat to filter all subjects containing "Mountain" of an RDS database.
Prefix lgdr:<http://linkedgeodata.org/triplify/> Prefix lgdo:<http://linkedgeodata.org/ontology/>
Select * where {
?s ?p ?o .
filter (contains(?s, "Mountain"))
} Limit 1000
The query leads to an error:
Virtuoso 22023 Error SL001: The SPARQL 1.1 function CONTAINS() needs a string value as first argument
You can get it to "work" using:
Prefix lgdr:<http://linkedgeodata.org/triplify/> Prefix lgdo:<http://linkedgeodata.org/ontology/>
Select * where {
?s ?p ?o .
filter (contains(str(?s), "Mountain"))
} Limit 1000
Note the additional str in the query.
However, that results in
Virtuoso S1T00 Error SR171: Transaction timed out
and I am not sure how to deal with that.
But in principle in works: When you use
Limit 1
you get
s p o
http://linkedgeodata.org/ontology/MountainRescue http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#Class
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)
}
I have two graphs with similar data but a slight difference.
my goal is to merge them using SPARQL and perform the integration. I want a final output of two RDF graphs that has a slight difference in a single RDF graph using SPARQL.
example one graph is :
ns0:BaseRoleClassLib
a ns0:ExternalReference ;
ns0:externalReferenceAlias "BaseRoleClassLib" .
ns0:maxTransportationWeight
a ns0:Attribute ;
schema:name "maxTransportationWeight" ;
ns0:hasValue "35" .
second graph is :
ns0:BaseRoleClassLib
a ns0:ExternalReference ;
ns0:maxTransportationWeight
a ns0:Attribute ;
schema:name "maxTransportationWeight" ;
ns0:hasValue "35.0" .
The only difference is one has the transport value in integer and other in the float.
So I write a query to generalise them :
select distinct ?integer
from <graph1>
from <graph2>
where {
?s ns0:hasValue ?y
Bind(xsd:integer(xsd:decimal(?y)) as ?integer)
}
}
This converts the difference in to generalised form of integer.
Now my next goal is I want to integrate these files into a single RDF by using the above result .
I want an RDF file which has the union of these and the solved generalization of float to integer.
S1 , S2 -> generalization -> integration -> s3 RDF
How can I achieve this using SPARQL constructor / insert ?
Thanks so much
This can be done pretty straightforwardly by CONSTRUCT. SPARQL update doesn't seem to support FROM, so you'd need to use a UNION of GRAPH statements. The following should get the merge you are looking for - basically filter out the old ns0:hasValue value and insert the new one:
CONSTRUCT {
?s ?p ?o .
?s ns0:hasValue ?intValue .
}
FROM <graph1>
FROM <graph2>
WHERE {
?s ?p ?o .
OPTIONAL{?s ns0:hasValue ?origValue .}
BIND(IF(datatype(?origValue) = xsd:integer, ?origValue, xsd:integer(STRBEFORE(str(?origValue), ".")) )as ?intValue)
FILTER (?p != ns0:hasValue)
}
Note that conversion of float to integer isn't straightforward. You's have to live with rounding down or have logic to round by decimal values.
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.
I want to select a triple using SPARQL. To do it, i'm using following query:
SELECT count (*)
WHERE {?s ?p ?o}
FILTER (?s=http://kjkhlsa.net && ?p=http://lkasdjlkjas.com && ?o=Test)
As answer i get fully wrong triple :( subject ist not equal to "http://kjkhlsa.net", predicate is not equal to "http://lkasdjlkjas.com" and object ist also not equal to "Test". Can someone explain me, what I'm doing wrong :(
edit1:
I have put the query into php file:
$inst_query = 'SELECT * { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test"}';
echo $inst_query;
The answer from the echo was "SELECT * { "Test"}". Then i tried it with WHERE:
$inst_query = 'SELECT * WHERE { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test"}';
echo $inst_query;
Here was the answer "SELECT * WHERE { "Test"}"...so, i'm missing the URIs, but this seems for me as php issue and not sparql problem.
edit2:
I've put the query into SPARQL Query editor and i get the response "no result"....but I'm sure, that i have this triple.
In its current form the question is not very clear (see my comment above).
Since you are essentially trying to get triples matching a pattern, it is more efficient to use a graph pattern instead of FILTER. Many SPARQL implementations first match candidate triples by graph patterns and only then apply the FILTER expression. In essence, with a ?s ?p ?o graph pattern, you're doing a linear scan over all your triples.
So, here's something that should work, using graph patterns instead of FILTER.
SELECT * { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test" }
Notes: I didn't include COUNT(*) which is not standard SPARQL. <> around URIs. "" around literal.
Try to use this :
SELECT count (*) as ?count
WHERE {
?s ?p ?o
FILTER (?s=<http://kjkhlsa.net> && ?p=<http://lkasdjlkjas.com> && ?o=Test)
}
The following query uses the count function to count the number of distinct URI(s) returned to the ?s variable.
SELECT ?s (COUNT (DISTINCT ?s) as ?count)
WHERE {?s ?p ?o}
FILTER (?o="Test")