简体   繁体   中英

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.

For example with 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.

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. 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 . 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/ . 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 |
+--------------+--------+--------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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