Search code examples
pythonsparqlrdflibowlready

How to get only value "Tuýp_2" with sparql query and remove URIRef link "http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#Tuýp_2"


I'm new to rdflib I trying to get only the value Tuýp_2, not include the IRI http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#Tuýp_2

def testontology():
    test = """PREFIX : <http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#> SELECT ?a   WHERE { <http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#Benhnhan001>:coLoaiTieuDuong ?a}"""
    r = list(graph.query(test))
    for item in r:
        result = str(item['a'])
    return render_template("testontology.html", values=result)

The result from the query "test" tried in protege is good result picture

when I use graph.query it return the value included the iri

I tried with SELECT (str(?b) as ?a) as well but the result still the same I think the problem is I dont know much about get value with graph.query in rdflib

Please help me to query the value


Solution

  • Solved with

    PREFIX : <http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#> 
    SELECT ?aTxt ?a   WHERE { <http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#Benhnhan001>:coLoaiTieuDuong ?a}.
    bind(strafter(str(?a),"http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#") as ?aTxt)}