简体   繁体   English

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

[英]Return values under same column in SPARQL query

Given three possible objects for triples, foaf:name , foaf:givenName , and foaf:familyName , where statements either have foaf:name or foaf:givenName + foaf:familyName , eg:给定三个可能的三元组对象, foaf:namefoaf:givenNamefoaf:familyName ,其中语句要么有foaf:name要么foaf:givenName + foaf:familyName ,例如:

<uri1> <foaf:name> "Lolly Loozles" .

<uri2> <foaf:givenName> "Stotly" .
<uri2> <foaf:familyName> "Styles" .

wondering how to write a SPARQL query to return a new variable like pretty_name that is either the value of foaf:name or a concatenation of the values from foaf:givenName and foaf:familyName .想知道如何编写一个 SPARQL 查询来返回一个新变量,比如pretty_name ,它要么是foaf:name的值,要么是来自foaf:givenNamefoaf:familyName的值的串联。

Resulting in something like:导致类似:

?o     | ?pretty_name
----------------------
<uri1> | Lolly Loozles
<uri2> | Stotly Styles

This is what I have so far, but unsure how to proceed:到目前为止,这是我所拥有的,但不确定如何进行:

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 ) .

}

I had imagined, and tried, adding another BIND to add to ?pretty_name , but the SPARQL engine wouldn't have it:我曾想象并尝试添加另一个BIND以添加到?pretty_name ,但 SPARQL 引擎没有它:

BIND( ?foaf_fullName AS ?pretty_name ) .

I also had luck writing a CONSTRUCT statement to get the values I'm looking for, but don't have the ability to write back to this triplestore (for a number of reasons):我也很幸运地写了一个CONSTRUCT语句来获取我正在寻找的值,但没有能力写回这个三元组(出于多种原因):

CONSTRUCT { 
    ?s :hasPrettyName ?foaf_fullName .
    ?s :hasPrettyName ?pretty_name .
}

I had thought that CONSTRUCT could accompany SELECT , but must have been mistaken?我原以为CONSTRUCT可以伴随SELECT ,但一定是错的?

Any insight or suggestions would much appreciated.任何见解或建议将不胜感激。

Using @StanislavKralin comment/suggestion to use COALESCE without IF clauses works great:使用@StanislavKralin 评论/建议在没有IF子句的情况下使用COALESCE效果很好:

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 )

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM