diff options
| author | Gunnar Aastrand Grimnes <gromgull@gmail.com> | 2013-04-25 13:39:53 +0200 |
|---|---|---|
| committer | Gunnar Aastrand Grimnes <gromgull@gmail.com> | 2013-04-25 13:39:53 +0200 |
| commit | bb10be680fcd61492201b0ece87e841d521cca71 (patch) | |
| tree | 8adb6addcb52f5171095ad0b81edc98ce4d5e994 /examples/sparql_query_example.py | |
| parent | 9f0814e3050b040f531092f985dc8c6539e9e351 (diff) | |
| download | rdflib-bb10be680fcd61492201b0ece87e841d521cca71.tar.gz | |
example cleanup - copy examples from rdflib-sparql
Diffstat (limited to 'examples/sparql_query_example.py')
| -rw-r--r-- | examples/sparql_query_example.py | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/examples/sparql_query_example.py b/examples/sparql_query_example.py index ed70cf7e..285eabf1 100644 --- a/examples/sparql_query_example.py +++ b/examples/sparql_query_example.py @@ -1,31 +1,27 @@ -from rdflib import Literal, ConjunctiveGraph, Namespace, BNode -import rdflib +""" + +Query using graph.query + +Result is iterable over the result rows + +The variable bindings can be access +as attributes of the row objects +For variable names that are not valid python identifiers, +dict access (i.e. with row[var] / __getitem__) is also possible. + +result.vars contains the variables -# Note that this example uses SPARQL and assumes you have -# rdfextras/rdflib-sparql installed, no other steps are nescessary, -# the SPARQL implementation should be found automatically. - -DC = Namespace(u"http://purl.org/dc/elements/1.1/") -FOAF = Namespace(u"http://xmlns.com/foaf/0.1/") - -graph = ConjunctiveGraph() -s = BNode() -graph.add((s, FOAF['givenName'], Literal('Alice'))) -b = BNode() -graph.add((b, FOAF['givenName'], Literal('Bob'))) -graph.add((b, DC['date'], Literal("2005-04-04T04:04:04Z"))) - -q=""" -PREFIX foaf: <http://xmlns.com/foaf/0.1/> -PREFIX dc: <http://purl.org/dc/elements/1.1/> -PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> -SELECT ?name -WHERE { ?x foaf:givenName ?name . - OPTIONAL { ?x dc:date ?date } . - FILTER ( bound(?date) ) - } """ -for x in graph.query(q): - print x +import rdflib + +g = rdflib.Graph() +g.load("foaf.rdf") + +for row in g.query( + 'select ?s where { [] <http://xmlns.com/foaf/0.1/knows> ?s .}'): + print row.s + # or row["s"] + # or row[rdflib.Variable("s")] + |
