Given the following Turtle:
prefix : <http://example.org/>
prefix blank: <http://example.org/blank>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
:alice :friends blank:b1 .
blank:b1 rdf:first "Alice" ;
rdf:rest blank:b2 .
blank:b2 rdf:first "Bob" ;
rdf:rest blank:b3 .
blank:b3 rdf:first "Carol" ;
rdf:rest rdf:nil .
I would like to create a SPARQL query that translates all URIs like :b1
, :b2
, ... to blank nodes like: _:b1
, _:b2
, ...
So the expected result would be:
prefix : <http://example.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
:alice :friends _:b1 .
_:b1 rdf:first "Alice" ;
rdf:rest _:b2 .
_:b2 rdf:first "Bob" ;
rdf:rest _:b3 .
_:b3 rdf:first "Carol" ;
rdf:rest rdf:nil .
which would be the equivalent to:
prefix : <http://example.org/>
prefix blank: <http://example.org/blank>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
:alice :friends ("Alice" "Bob" "Carol" ).
An initial attempt that I tried is:
prefix ex: <https://example.com/ns#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
CONSTRUCT {
?bnode1 rdf:first ?value ;
rdf:rest ?bnode2 .
?s ?p ?o .
}
where {
{ ?iri1 rdf:first ?value ;
rdf:rest ?iri2 . }
BIND(BNODE(str(?iri1)) as ?bnode1)
BIND(BNODE(str(?iri2)) as ?bnode2)
}
But the semantics of BNODE
according to SPARQL spec indicates that it generates a new blank node for each solution mapping, so the generated blank nodes are not linked. For example, using Jena, I obtained:
@prefix : <http://example.org/> .
@prefix blank: <http://example.org/blank> .
@prefix ex: <https://example.com/ns#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
[ rdf:first "Carol" ;
rdf:rest []
] .
[ rdf:first "Alice" ;
rdf:rest []
] .
[ rdf:first "Bob" ;
rdf:rest []
] .
which is not what I want.