给出三个可能的三元组对象foaf:name
,foaf:givenName
和foaf:familyName
,其中语句具有foaf:name
或 {{1} } + foaf:givenName
,例如:
foaf:familyName
想知道如何编写SPARQL查询以返回诸如<uri1> <foaf:name> "Lolly Loozles" .
<uri2> <foaf:givenName> "Stotly" .
<uri2> <foaf:familyName> "Styles" .
的新变量,该变量要么是pretty_name
的值,要么是foaf:name
和foaf:givenName
中的值的并置
结果类似:
foaf:familyName
这是我到目前为止的内容,但不确定如何进行:
?o | ?pretty_name
----------------------
<uri1> | Lolly Loozles
<uri2> | Stotly Styles
我曾想过,然后尝试将另一个PREFIX : <https://example.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
# select two variables, not ideal...
SELECT ?foaf_fullName ?pretty_name
WHERE {
# Find all triples
?s ?p ?o .
# Binds
OPTIONAL { ?s foaf:name ?foaf_fullName }
OPTIONAL { ?s foaf:givenName ?givenName }
OPTIONAL { ?s foaf:familyName ?familyName }
# Filter where predicate is part of list
FILTER (?p IN (foaf:name, foaf:givenName, foaf:familyName ) )
# Binds
BIND( CONCAT(?givenName, ' ', ?familyName) AS ?pretty_name ) .
}
添加到BIND
中,但是SPARQL引擎却没有它:
?pretty_name
我也很幸运编写了BIND( ?foaf_fullName AS ?pretty_name ) .
语句来获取我要查找的值,但是由于某些原因,我无法写回该三元组:
CONSTRUCT
我以为CONSTRUCT {
?s :hasPrettyName ?foaf_fullName .
?s :hasPrettyName ?pretty_name .
}
可以和CONSTRUCT
一起出现,但一定是误会了吗?
任何见解或建议将不胜感激。
答案 0 :(得分:1)
使用@StanislavKralin注释/建议使用不带COALESCE
子句的IF
很有用:
PREFIX : <https://example.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
# select two variables, not ideal...
SELECT ?foaf_fullName ?pretty_name
WHERE {
# Find all triples
?s ?p ?o .
# Binds
OPTIONAL { ?s foaf:name ?foaf_fullName }
OPTIONAL { ?s foaf:givenName ?givenName }
OPTIONAL { ?s foaf:familyName ?familyName }
# Filter where predicate is part of list
FILTER (?p IN (foaf:name, foaf:givenName, foaf:familyName ) )
# Binds
BIND( COALESCE(?foaf_fullName, CONCAT(?givenName, ' ', ?familyName)) AS ?pretty_name )
}