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
371 views
in Technique[技术] by (71.8m points)

owl - How to concatenate two variables ogether and output a string in SPARQL

I have three variables ?leaf and ?superclass, and ?supersuperclass containing uri's. Now I would like to end up with a table with 2 columns, one column being the ?leaf label and the other being a concatenation of ?superclass and ?supersuperclass with a - in between them.

For instance, a fictional example: | leaf?| ?superclass-supersuperclass| |:----: | :-----:| | bed | bedroom-house | |fridge| kitchen-house| |diningroom|house-street|

I have tried concatenating superclass and supersuperclass in the select portion of my query, but this didn't work. I'm not sure where else I could place it. Do I need to create a new variable that somehow assigns the string values of each variabe to that new variable, concatenated? I do know how to access the labels of each uri, which is the string I end up wanting to concatenate.

tldr: how to concatenate the string values within 2 variables?

Edit: My query up to now: I hope it's readable, removing the comments may help, i mostly wrote those for myself.

 PREFIX owl: <http://www.w3.org/2002/07/owl#>
    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX : <*databaselocation*>

    SELECT DISTINCT ?leaf  #?subclasslabel ?subclasslabel #?superclasslabel ? 
    supersuperclasslabel 
    WHERE {GRAPH <*insertyourgraphhere*>

    #a leaf is the lowest level class: A class that does not have a subclass
    {{
            #I want anything which is a class
            ?leaf rdf:type owl:Class.
            #i also want the subclass of any superclass if that exists
            optional {?leaf rdfs:subClassOf ?superclass .}
            
            #give me the label of that subclass
            #optional {?leaf skos:prefLabel ?subclasslabel.}

        
            #?superclass rdfs:subClassOf ?supersuperclass.
            #give me the label of the superclass
            #?superclass skos:prefLabel ?superclasslabel.

            
            #if it exists, give me also the superclass of the superclass 
            creating a supersuperclass
            # {?superclass rdfs:subClassOf ?supersuperclass.
            #give me the label of the supersuperclass
            # {?supersuperclass skos:prefLabel ?supersuperclasslabel.}
                
            
            #keep the leafs that are NOT The values whereby the subclass is 
     not empty. (double negative for remove the leafs where the subclass has a 
    subclass below it)
    FILTER NOT EXISTS
    {?subclass rdfs:subClassOf ?leaf 
                FILTER (?sublass != ?leaf && ?subclass != owl:Nothing ) }}}}    
question from:https://stackoverflow.com/questions/65919428/how-to-concatenate-two-variables-ogether-and-output-a-string-in-sparql

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

...