SPARQL query to remove "#en" - sparql

I have a large skos taxonomy that has some incorrect notation properties. Most of the properties are xsd:string but some appear with a "#en" language string. I want to modify the triples so as to remove the language string from these triples and convert them to xsd:string.
I tried the query below. It doesn't report any errors and commits successfully.
DELETE { ?s ?p ?o }
INSERT { ?s ?p ?o2 }
WHERE
{
?s skos:notation ?o .
BIND(STRDT(STR(?o), xsd:string) AS ?o2)
}
However, the query does not result in any change to the triples data. Can anyone suggest where I might be going wrong?

Variable ?p in your query appears to be unbound. Try:
DELETE { ?s skos:notation ?o }
INSERT { ?s skos:notation ?o2 }
WHERE
{
?s skos:notation ?o .
BIND(STRDT(STR(?o), xsd:string) AS ?o2)
}

Related

Find orphan nodes with SPARQL

I am trying to find orphan nodes (nodes which do not have any incoming relations) with SPARQL in a Fuseki database.
I tried several queries which all do not return correct results.
I tried the following:
Query 1 (got this from linkedIn)
select ?o ?isOrphan where { GRAPH <http://localhost:8080/catalog/-1305288727> {
?s ?p ?o .
FILTER(!isLiteral(?o))
bind(!(EXISTS {?o ?p1 ?o2}) as ?isOrphan)}}
Query 2
SELECT ?source ?s ?p ?o
WHERE { GRAPH <http://localhost:8080/catalog/-1305288727>{
?s ?p ?o .
FILTER EXISTS {?source ?p ?s } .
}
}
Query 3 - unbound variable pp in FILTER
SELECT ?source ?s ?p ?o
WHERE { GRAPH <http://localhost:8080/catalog/-1305288727>{
?s ?p ?o .
FILTER EXISTS {?source ?pp ?s } .
}
}
Any help is highly appreciated.
This query finds each entity that is the subject of any triple, and then checks that this entity is not the object of any triple.
SELECT ?orphan
FROM <http://localhost:8080/catalog/-1305288727>
WHERE {
?orphan ?p1 [] .
FILTER NOT EXISTS { ?linkingNode ?p2 ?orphan . }
}

Is there a good example of how to use SPARQL to replace a substring with another substring across a collection of triples?

I want to edit a set of URIs replacing a substring "iso-693" with "iso-639" using a SPARQL query. I am using REPLACE but it doesn't seem to do anything.
I have a large SKOS taxonomy with URIs that have an incorrect string. They should have this string: "iso-639" but I made a mistake when creating it and put "iso-693". I'd like to correct it. I used the SPARQL query shown below, which when run returns a message "update successful", but none of the triples data actually changes. Where am I going wrong?
INSERT
{
?s ?p ?o2
}
WHERE
{
?s ?p ?o .
FILTER (regex(str(?s), "iso-693") || regex(str(?o), "iso-693"))
BIND(REPLACE(?o, "iso-693", "iso-639", "i") AS ?o2) .
}
I expected all of the occurrences of the substring to change to the desired value, but nothing seems to change at all despite the success message.
You are missing the bit that removes the old value (INSERT just adds a new triples). To replace a triple, you should DELETE the old triple at the same time as you are INSERTing the new one, like this:
DELETE
{
?s ?p ?o
}
INSERT
{
?s ?p ?o2
}
WHERE
{
?s ?p ?o .
FILTER (regex(str(?s), "iso-693") || regex(str(?o), "iso-693"))
BIND(REPLACE(?o, "iso-693", "iso-639", "i") AS ?o2) .
}
If you are targeting URIs then you need to construct new IRIs with the required substitution and use these in the INSERT part of the update along with the original values fro ?s and ?o for DELETE part. The REPLACE will produce Literals which is not correct fro subjects.
Suggest using something along following lines:
DELETE {
?s ?p ?o
}
INSERT {
?newS ?p ?newO
} WHERE {
?s ?p ?o .
bind("iso-693" as ?match) .
bind("iso-639" as ?replacement) .
bind (regex(str(?s), ?match) as ?subjMatch) .
bind (regex(str(?o), ?match) as ?objMatch) .
filter (?subjMatch || ?objMatch)
bind (if(?subjMatch, IRI(replace(str(?s), ?match, ?replacement)), ?s) as ?newS)
bind (if(?objMatch, IRI(replace(str(?o), ?match, ?replacement)), ?o) as ?newO)
}

Sparql CONSTRUCT with DISTINCT

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

SPARQL: Querying the Default Graph

With the RDF query language SPARQL, I'm trying to find a way to do a boolean query (or any other query) for anything not in a Named Graph.
ASK { GRAPH null { ?s ?p ?o } }
Can't find really any documentation on searching specifically within an empty Named Graph. Also tried replacing null with <>, empty, and (nothing).
This query will look for triples in the default graph, then remove ones that are also in a named graph:
SELECT ?s ?p ?o {
?s ?p ?o
FILTER NOT EXISTS { GRAPH ?g { ?s ?p ?o } }
}

Sparql query delete all statements

I would like to delete all statements related to an object that contains certain characters in the label. I am using the query:
DELETE
{?term ?p ?o}
WHERE
{
?term rdfs:label ?label.
FILTER(regex(?label, "xx", "i"))
?term ?p ?o.
}
However, this query seems to fail to delete all the statements that contain the subject of this statement as object. Then I seem to need another query.
DELETE
{?s ?p ?term}
WHERE
{
?term rdfs:label ?label.
FILTER(regex(?label, "xx", "i"))
?s ?p ?term.
}
The SELECT * does not seem to work for DELETE, and I have also tried to model a UNION within DELETE with no success. Could you please point me to the solution? Many thanks.
try this. it worked for me both for insert and delete
DELETE
{?term ?p ?o}
WHERE
{
SELECT ?term ?p ?o
WHERE{
?term rdfs:label ?label.
FILTER(regex(?label, "xx", "i"))
}
}