Select All That Has The Same Prefix SPARQL [duplicate] - sparql

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.

Related

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

SPARQL query for returning individual data [duplicate]

This question already has an answer here:
SPARQL Query: How I get only a literal or string as result?
(1 answer)
Closed 6 years ago.
I have the following query to return all the properties with their respective values using SPARQL:
select distinct ?property ?value
where {
?instance a df:Tools .
?instance ?property ?value }
The example of the output is:
Name "EnCase"^^<http://www.w3.org/2001/XMLSchema#string>
Is there a way to make the value field only output: "EnCase", instead of the entire property value? I am new to SPARQL and ontologies, but this would help me out a lot.
You can use STR to convert convert the value to a plain literal:
select distinct ?property ?plain_value
where {
?instance a df:Tools .
?instance ?property ?value
BIND(STR(?value) AS ?plain_value)
}

SPARQL Query final label [duplicate]

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)
}

querying RDFs using SPARQL query

Assuming we have a dataset including the following 3 RDF triples. I want to retrieve all these three triples using sparql query.
http://test.com/11/META_C1-INST predicate1 http://test.com/NO_CONTEXT/META_C0059714-INST
http://test.com/23/META_C1-INST predicate1 http://test.com/NO_CONTEXT/META_C0059714-INST
http://test.com/43/META_C1-INST predicate1 http://test.com/NO_CONTEXT/META_C0142817-INST
Here is the query I wrote but I know it is not right:
prefix pred:<http://test.com/>
select distinct *
where {pred:META_C1-INST ?p ?o};
How should I say: "Look for any triples with the subject from http://test.com/??/ where ?? can be anything like 11, 23, S23?
You can transform the URI into a string and then filter based on what it starts with. The keyword you need to use is STRSTARTS:
select distinct *
where{
?s ?p ?o
filter(STRSTARTS(str(?s), "http://test.com/"))
}
or you can use regular expressions with the ^ sign:
select distinct *
where{
?s ?p ?o
FILTER regex(str(?s), "^http://test.com/") .
}

SPARQL Query problem -> wrong answer

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")