Consider the following SPARQL query:
PREFIX basics: <http://example.org/basic-knowledge/>
PREFIX special: <http://example.org/special-predicates/>
SELECT ?S ?P ?O
WHERE
{
?S ?P ?O.
}
This yields all triples.
How can I restrict the result to those triples where the predicate ?P is from the namespace with the prefix special:?
This can be done with a string test. str() turns URIs into strings.
FILTER(strStarts(str(?P), str(special:)))
note the special: is replaced by the parser with the full URI: http://example.org/special-predicates so the executiuon is
FILTER(strStarts(str(?P), str(<ttp://example.org/special-predicates>)))
Runnable example:
PREFIX ns1: <http://example/>
PREFIX ns2: <http://ex/>
SELECT * {
VALUES ?x { ns1:abc ns2:xyz }
FILTER(strstarts(str(?x), str(ns1:)))
}
Related
i want to create a construct query with triples of subjects, that has a certain subclass.
That works fine:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX AS: <http://www.w3.org/ns/activitystreams#>
CONSTRUCT {?s ?p ?o}
WHERE {
?s rdf:type/rdfs:subClassOf* AS:Create ;
?p ?o .
}
But now i want to ask for more than one type!
Something like
WHERE {
?s rdf:type/rdfs:subClassOf* AS:Create|AS:Announce ;
?p ?o .
}
any idea ?
You can use a VALUES clause for this:
VALUES ?cls {AS:Create AS:Announce} ?s rdf:type/rdfs:subClassOf* ?cls ;
Given three possible objects for triples, foaf:name, foaf:givenName, and foaf:familyName, where statements either have foaf:name or foaf:givenName + foaf:familyName, e.g.:
<uri1> <foaf:name> "Lolly Loozles" .
<uri2> <foaf:givenName> "Stotly" .
<uri2> <foaf:familyName> "Styles" .
wondering how to write a SPARQL query to return a new variable like pretty_name that is either the value of foaf:name or a concatenation of the values from foaf:givenName and foaf:familyName.
Resulting in something like:
?o | ?pretty_name
----------------------
<uri1> | Lolly Loozles
<uri2> | Stotly Styles
This is what I have so far, but unsure how to proceed:
PREFIX : <https://example.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
# select two variables, not ideal...
SELECT ?foaf_fullName ?pretty_name
WHERE {
# Find all triples
?s ?p ?o .
# Binds
OPTIONAL { ?s foaf:name ?foaf_fullName }
OPTIONAL { ?s foaf:givenName ?givenName }
OPTIONAL { ?s foaf:familyName ?familyName }
# Filter where predicate is part of list
FILTER (?p IN (foaf:name, foaf:givenName, foaf:familyName ) )
# Binds
BIND( CONCAT(?givenName, ' ', ?familyName) AS ?pretty_name ) .
}
I had imagined, and tried, adding another BIND to add to ?pretty_name, but the SPARQL engine wouldn't have it:
BIND( ?foaf_fullName AS ?pretty_name ) .
I also had luck writing a CONSTRUCT statement to get the values I'm looking for, but don't have the ability to write back to this triplestore (for a number of reasons):
CONSTRUCT {
?s :hasPrettyName ?foaf_fullName .
?s :hasPrettyName ?pretty_name .
}
I had thought that CONSTRUCT could accompany SELECT, but must have been mistaken?
Any insight or suggestions would much appreciated.
Using #StanislavKralin comment/suggestion to use COALESCE without IF clauses works great:
PREFIX : <https://example.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
# select two variables, not ideal...
SELECT ?foaf_fullName ?pretty_name
WHERE {
# Find all triples
?s ?p ?o .
# Binds
OPTIONAL { ?s foaf:name ?foaf_fullName }
OPTIONAL { ?s foaf:givenName ?givenName }
OPTIONAL { ?s foaf:familyName ?familyName }
# Filter where predicate is part of list
FILTER (?p IN (foaf:name, foaf:givenName, foaf:familyName ) )
# Binds
BIND( COALESCE(?foaf_fullName, CONCAT(?givenName, ' ', ?familyName)) AS ?pretty_name )
}
PREFIX content: <http://example.com/content#>
construct { ?s content:field ?o}
WHERE { ?s content:field ?o }
90% of all the ?o I get here are the same URI <http://example.com/name>.
I'm trying to find a way to filter out all quads that have the same value for ?o, so in the end I get a list of quads which are unique by its ?o
I tried DISTINCT ?o CONSTRUCT{...} but from what I saw you cant use DISTINCT on a CONSTRUCT.
How would you filter the returned list of quads
I'm trying to find a way to filter out all quads that have the same
value for ?o, so in the end I get a list of quads which are unique by
its ?o
if it does not matter which exact value is bound to ?s, then a sub-select with a group by ?o is the way to go. Use (SAMPLE(?s) as ?subj) e.g. something like:
`
PREFIX content: <http://example.com/content#>
construct { ?s content:field ?o}
WHERE {
{ select ?o (SAMPLE(?subj) as ?s)
{ ?subj content:field ?o }
group by ?o
}
}
`
I'm a naive user trying to replicate a query that results in the following type of string:
derives_from some (epithelial cell and (part_of some (uterine cervix and (part_of some (Homo sapiens and (has disease some adenocarcinoma)))))
I'm at a hackathon, we have no ontology/SPARQL expert, and we're just trying to get these related fields out of this ontology and into SOLR. We're desperate!
The webpage this is from
http://www.ontobee.org/ontology/CLO?iri=http://purl.obolibrary.org/obo/CLO_000368)
helpfully provides all the SPARQL queries that are used on the page. I think this is the relevant query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT DISTINCT ?ref ?refp ?label ?o FROM <http://purl.obolibrary.org/obo/merged/CLO> WHERE {
?ref ?refp ?o .
FILTER ( ?refp IN ( owl:equivalentClass, rdfs:subClassOf ) ) .
OPTIONAL { ?ref rdfs:label ?label } .
{
{
SELECT ?s ?o FROM <http://purl.obolibrary.org/obo/merged/CLO> WHERE {
?o ?p ?s .
FILTER ( ?p IN ( rdf:first, rdf:rest, owl:intersectionOf, owl:unionOf, owl:someValuesFrom, owl:hasValue, owl:allValuesFrom, owl:complementOf, owl:inverseOf, owl:onClass, owl:onProperty ) )
}
}
OPTION ( TRANSITIVE, t_in( ?s ), t_out( ?o ), t_step( ?s ) as ?link ).
FILTER ( ?s= <http://purl.obolibrary.org/obo/CLO_0003684> )
}
}
ORDER BY ?label
However, I can't even run that to check, because my SPARQL endpoint doesn't support Virtuoso. http://sparql.bioontology.org
So, it spits back an error on OPTION(TRANSITIVE).
Can anyone show me the equivalent standard pathway language? There are varying pathway lengths between the target nodes.
Virtuoso's transitivity operator is more powerful than what standard SPARQL provides, so in the general case it will not always be possible to express the same thing in a single standard SPARQL query. However, I believe that in this case it is possible.
The following property path would be the equivalent (disclaimer, I haven't tested this query, but it should give you the general idea):
?o (rdf:first|rdf:rest|owl:intersectionOf|owl:unionOf|owl:someValuesFrom|owl:hasValue|owl:allValuesFrom|owl:complementOf|owl:inverseOf|owl:onClass|owl:onProperty)+ ?s .
The full query would become something like this:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT DISTINCT ?ref ?refp ?label ?o FROM <http://purl.obolibrary.org/obo/merged/CLO> WHERE {
?ref ?refp ?o .
FILTER ( ?refp IN ( owl:equivalentClass, rdfs:subClassOf ) ) .
OPTIONAL { ?ref rdfs:label ?label } .
{
{
SELECT ?s ?o FROM <http://purl.obolibrary.org/obo/merged/CLO> WHERE {
?o (rdf:first|rdf:rest|owl:intersectionOf|owl:unionOf|owl:someValuesFrom|owl:hasValue|owl:allValuesFrom|owl:complementOf|owl:inverseOf|owl:onClass|owl:onProperty)+ ?s .
}
}
FILTER ( ?s= <http://purl.obolibrary.org/obo/CLO_0003684> )
}
}
ORDER BY ?label
Note, by the way, that the FILTER condition on your variable ?s is outside the subselect, which might make this query a bit of a performance hog. Since you are not using ?s anywhere else in the query, you can simplify this part of the query further, eliminating the FILTER, like so:
{
{
SELECT ?o FROM <http://purl.obolibrary.org/obo/merged/CLO> WHERE {
?o (rdf:first|rdf:rest|owl:intersectionOf|owl:unionOf|owl:someValuesFrom|owl:hasValue|owl:allValuesFrom|owl:complementOf|owl:inverseOf|owl:onClass|owl:onProperty)+ <http://purl.obolibrary.org/obo/CLO_0003684> .
}
}
}
I am try to Implement a Sparql Query which will give some result.I am trying to Implement Like this:
My Data points are below from where I getting the data:
Subject:
<http://rhizomik.net/semanticxbrl/0001397832_agph-20110930/Context_9ME_30-Sep-2011/ConvertibleNotesPayableTextBlock/>
Predicate:
<http:// www.w3.org/1999/02/22-rdf-syntax-ns#type>
Object:
<http://www.atlanticgreenpower.com/20110930#ConvertibleNotesPayableTextBlock>
My Query is
PREFIX ab: <http:// www.atlanticgreenpower.com/20110930#>
SELECT ?node ?val_type ?value
WHERE {
?node ab:val_type ?val_type .
?node ab:value ?value .
}
I want to get the result of all subject predicate and object.I am new to sparql.please help me out
In SPARQL everything with a ? in front of it is a variable. If you want everything from the subject, predicate and object you can do it like this:
SELECT ?s ?p ?o WHERE { ?s ?p ?o }
But then you list everything. When you want to only list the values of your given subject you will have to do it like this:
SELECT * WHERE { <http://rhizomik.net/semanticxbrl/0001397832_agph-20110930/Context_9ME_30-Sep-2011/ConvertibleNotesPayableTextBlock/> ?p ?o }
And your query could easily be formatted to something like this:
PREFIX ab: <http:// www.atlanticgreenpower.com/20110930#>
SELECT ?s ?valType ?value WHERE {
?s ab:val_type ?valType ;
ab:value ?value .
}
Where ; marks when you want to include something else in your query with your given subject, but . marks the end of your query. If you want more information about how SPARQL works or how you can query check out the Euclid project with webinar recording: Querying linked data, 2013-03-04 or just test it yourself on a SPARQL endpoint like from FactForge.
To reflect my comment below:
SELECT * WHERE { ?s ?p <http://www.atlanticgreenpower.com/20110930#ConvertibleNotesPayableTextBlock> }
will select everything with the given object.
Try this:
SELECT * WHERE
{
?s ?p <http://www.atlanticgreenpower.com/20110930#ConvertibleNotesPayableTextBlock>
}