Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

semantic web - SPARQL query to get all class label with namespace prefix defined

I want to get all class that stored in sesame repository.

This is my query

SELECT ?class ?classLabel
WHERE {
?class rdf:type rdfs:Class.
?class rdfs:label ?classLabel.
}

It's return all URI of class with label. For example,

"http://example.com/A_Class" "A_Class"
"http://example.com/B_Class" "B_Class"

But, if namespace prefix already defined I want to get label with namespace defined. For example if I already defined namespace prefix "ex" for "http://example.com/", the result become

"http://example.com/A_Class" "ex:A_Class"
"http://example.com/B_Class" "ex:B_Class"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You want to add a URI prefix to the front of a label string?

I think you might be confused about what URI prefixes do. They're just shorthand for full URIs, and aren't part of the URI, and they don't have any bearing on strings.

You can do something like what you want with

SELECT ?class (CONCAT("ex:", ?classLabel) AS ?label
WHERE {
   ?class rdf:type rdfs:Class.
   ?class rdfs:label ?classLabel.
}

But the prefix won't depend on the prefix of the URI.

You could have a chain of IF()s that tests the starting characters of STR(?class), but it will get ugly quickly:

BIND(IF(STRSTARTS(STR(?class), "http://example.com/"), "ex:", "other:") as ?prefix)

then

SELECT ... (CONCAT(?prefix, ?classLabel) AS ?label

There's almost certainly an easier way to get what you want than doing it in SPARQL though :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...