ueeieiie
ueeieiie

Reputation: 1562

How to filter by BlankNode id

I'm trying to run this query in the SPARQL playground on GraphDB.

select * where { 
    ?s content:value ?value .
    FILTER REGEX(str(?value), "_:x1697BD364N0abcdd866d54")
} 

?value are BNodes

I'm trying to filter all values where the BNode ID is: _:x1697BD364N0abcdd866d54

I've tried a few different ways to filter by the ID:

  1. FILTER REGEX(str(?value), "#bnode-id")
  2. FILTER (?value), "#bnode-id")

but none was a success.

Questions:

  1. How would you filter based on a bnode id?
  2. Is it possible to STR(bnode) ?

Upvotes: 3

Views: 1558

Answers (1)

Stanislav Kralin
Stanislav Kralin

Reputation: 11479

  1. How would you filter based on a bnode id label?

See the tag info. In short, one can't rely on blank node labels in SPARQL queries:

There need not be any relation between a label _:a in the result set and a blank node in the data graph with the same label.

An application writer should not expect blank node labels in a query to refer to a particular blank node in the data.

If it is hard to distinguish blank nodes by their properties etc. in SPARQL, one can use GraphDB's internal resource identifiers.

PREFIX ent: <http://www.ontotext.com/owlim/entity#>
SELECT * {
    ?s content:value ?value .
    ?s ent:id ?id .
    FILTER (?id = 424242)
}

or

PREFIX ent: <http://www.ontotext.com/owlim/entity#>
SELECT * {
    ?s content:value ?value 
    BIND (ent:id(?s) AS ?id)
    FILTER (?id = 424242)
}

  1. Is it possible to STR(bnode)?

No. From 17.4.2.5 str:

simple literal  STR (literal ltrl)
simple literal  STR (IRI rsrc)

Returns the lexical form of ltrl (a literal); returns the codepoint representation of rsrc (an IRI).

Upvotes: 4

Related Questions