1

Here is my problem: I'm trying using sparql to get the value and show only result for web user the problem is i only know how to get result include rdflib.term.Literal('AAA') Is there any way to get only the value AAA without rdflib.term.Literal ?

The code i use in python:

import rdflib
graph = rdflib.Graph()
graph.parse ("static/owl/rdfxmlontologyhotrobenhtieuduong.owl")
query = """PREFIX : <http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#>
SELECT ?a
   WHERE { <http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#Benhnhan001>:CoHoVaTen ?b
    BIND(STR(?b) AS ?a)
    }"""

@app.route('/testsql')
def testsql():
    return render_template("testsql.html", values1=users.query.all(), values2=admins.query.all())

@app.route('/testontology')
def testontology():
    r = graph.query(query)
    return render_template("testontology.html", values=r)

Code in testontology.html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test ontology</title>
</head>
<body>
    <h1>Test Ontology</h1>
    {{values}}
    {% for item in values %}
        <p>Class ontology: {{ item }}</p>
    {% endfor %}
</body>
</html>

Result I get: Test Ontology <rdflib.plugins.sparql.processor.SPARQLResult object at 0x0000027DD9F39400> Class ontology: (rdflib.term.Literal('Nguyen Minh Anh'),)

For the ontology I used to query Ontology

I found a post on stackoverflow (How can I use the RDFLIB module in Python to retrieve a value from an OWL file using SparQL?) the solution is print row['value'] but i cannot apply that solution to my code

ngocvy
  • 11
  • 3

1 Answers1

2

str(r[‘a’])

You can get the literal valubof any RDFliteral by just casti it to a string with Python’s str() method so just pass the value as above, not just r, to the template.

Nicholas Car
  • 1,164
  • 4
  • 7