sparqlrdfturtle-rdfblank-nodes

SPARQL Query to delete all blank nodes with in a Resource


I am writing a SPARQL query that should delete all triples within in this resource.

prefix oslc: <http://open-services.net/ns/core#>
prefix example: <http://rdf.company.com/ns/something/net#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix dcterms: <http://purl.org/dc/terms/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

example:Resource2
        a                oslc:ResourceShape ;
        oslc:describes   example:Resource2 ;
        oslc:property    [ a                      oslc:Property ;
                           oslc:isMemberProperty  true ;
                           oslc:name              "pgn" ;
                           oslc:occurs            oslc:Zero-or-one ;
                           oslc:valueType         xsd:integer ] ;
        dcterms:title    "Resource2"^^rdf:XMLLiteral .   

I have tried:

 DELETE
    { ?s  ?p   ?o } 
  WHERE 
    { 
      ?s  ?p   ?o .
      FILTER  ( ?s IN ( <http://rdf.company.com/ns/something/net#Resource2>))
    }

However, id does not delete the blank node within it:

[ a                      oslc:Property ;
  oslc:isMemberProperty  true ;
  oslc:name              "pgn" ;
  oslc:occurs            oslc:Zero-or-one ;
  oslc:valueType         xsd:integer ] 

It is pretty obvious because the filter specifies a specific subject and the blank node does not have that subject.

Any idea how can I delete the blank node as well?


Solution

  • I hope you do not have nested blank nodes. Try this:

    PREFIX example: <http://rdf.company.com/ns/something/net#>
    
    DELETE {
        ?s  ?p   ?o .
        ?o  ?p1  ?o1 .
    }
    WHERE {
        VALUES (?s) { (example:Resource2) }
        ?s  ?p  ?o .
        OPTIONAL {
            ?o  ?p1  ?o1  .
            FILTER (isBlank(?o)) 
        }
    }
    

    Related question: Delete blank node from ontology through SPARQL UPDATE.