问题
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:
FILTER REGEX(str(?value), "#bnode-id")
FILTER (?value), "#bnode-id")
but none was a success.
Questions:
- How would you filter based on a bnode id?
- Is it possible to STR(bnode) ?
回答1:
- How would you filter based on a bnode
idlabel?
See the blank-nodes 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)
}
- 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 ofrsrc
(an IRI).
来源:https://stackoverflow.com/questions/55440728/how-to-filter-by-blanknode-id