在SPARQL查询的同一列下返回值

时间:2019-04-15 16:22:30

标签: sparql rdf

给出三个可能的三元组对象foaf:namefoaf:givenNamefoaf: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:namefoaf: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一起出现,但一定是误会了吗?

任何见解或建议将不胜感激。

1 个答案:

答案 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 )

}