Reputation:
I am trying to create an RDF graph, in which:
if ?n1
exists then a triple of the form: <?x ex:n "1">
will be created.
Otherwise, a triple of the form <?x ex:n "0">
will be created.
My code is the following:
CONSTRUCT {?x ex:n ?result .}
WHERE {
?x ex:n ?n1 .
BIND (IF(?n1 =" *"^^xsd:string, "0", "1") AS ?result)
}
However, only the triples where the value of ?n1
exists are created (<?x ex:n "1">
).
I miss the triples of the form: <?x ex:n "0">
.
Why does it happen?
Upvotes: 0
Views: 896
Reputation: 7880
" *"^^xsd:string
will not check that your string is empty. Instead it literlly means "four spaces followed by an asterisk".
For checking that a string matches a pattern, you should use the SPARQL REGEX
function. So you may want to use REGEX(?n1, "^ *$")
, where ^
and $
match respectively the begin and end of the pattern and *
is the zero-or-more quantifier.
CONSTRUCT { ?x ex:n ?result . }
WHERE {
?x ex:n ?n1 .
BIND (IF(REGEX(?n1, "^ *$"), "0", "1") AS ?result)
}
EDIT: If you want also to retrieve values for ?x
where you dont have <?x ex:n ?n1>
but you do have <?id ex:p ?x>
, you can do something like this:
CONSTRUCT { ?x ex:n ?result . }
WHERE {
{ ?id ex:p ?x . } UNION { ?x ex:n ?n1 . }
BIND (IF(!BOUND(?n1) || REGEX(?n1, "^ *$"), "0", "1") AS ?result)
}
Upvotes: 2