简体   繁体   English

如何在SPARQL中分割字符串

[英]How to split a string in SPARQL

I'm trying to get the Name of a triple, to separate the PREFIX from the value, and to show these splitted strings in two different columns. 我正在尝试获取三元组的名称,以将PREFIX与值分开,并在两个不同的列中显示这些拆分的字符串。

For example with foaf:Person 例如使用foaf:Person

?prefix | ?name
foaf:   | Person

I saw there are some solutions with SUBSTR, but I don't know how to use it in my case. 我看到SUBSTR有一些解决方案,但是我不知道如何在我的情况下使用它。

Any idea ? 任何想法 ?

Thank you by advance :-) 预先感谢您:-)

There are no prefixed URIs in RDF, this is just a concept of presentation but not part of the data. RDF中没有前缀URI,这只是表示的概念,而不是数据的一部分。 What you get by simple SPARQL queries would be a resource with the full URI, ie for your example http://xmlns.com/foaf/0.1/Person . 通过简单的SPARQL查询得到的将是具有完整URI的资源,即对于您的示例http://xmlns.com/foaf/0.1/Person What you can do by string functions is to split by the namespace and a local name, as you already found out. 您已经发现,通过字符串函数可以执行的操作是按名称空间和本地名称进行拆分。

If you really want to have a prefixed URI, you need a prefix mapping first, eg foaf -> http://xmlns.com/foaf/0.1/ . 如果您确实希望有一个前缀URI,则首先需要一个前缀映射,例如foaf -> http://xmlns.com/foaf/0.1/ Then you could do the following: 然后,您可以执行以下操作:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?prefixedName 
       (strbefore(?prefixedName,":") as ?prefix) 
       (strafter(?prefixedName,":") as ?name) {

 # dummy value
 VALUES ?uri {<http://xmlns.com/foaf/0.1/Person> }

 # get a prefixed name of the URI as string literal
 BIND(replace(str(?uri), str(foaf:), "foaf:") as ?prefixedName)
}

Output: 输出:

+--------------+--------+--------+
| prefixedName | prefix |  name  |
+--------------+--------+--------+
| foaf:Person  | foaf   | Person |
+--------------+--------+--------+

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

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