Christopher Warner Studies and thoughts, usually in coherent fashion.

21Aug/080

SPARQL and Subquery; 3rd go

Well; I gave up and found an easier way using straight rdflib methods; another day wasted. The tutorials on rdflib are pretty bad. So you just can't jump straight in. WordPress formatting sucks, I know; prolly should upgrade.

from rdflib import ConjunctiveGraph as Graph, Namespace

DublinCore = Namespace("http://purl.org/dc/elements/1.1/")

g = Graph()
g.parse("sample.metadata", format="xml")

creator_list = list(g.subject_objects(DublinCore["creator"]))
for data, blank_node in creator_list:
# We get the dc:creator sequence
sequence = g.seq(blank_node)
for creator in sequence:
print creator

21Aug/080

SPARQL and still sub-querying

for x in g.query("SELECT ?xcreator WHERE { ?creator dc:creator ?xcreator }",
initNs=ns):

print x

sub_query = "SELECT ?creator WHERE { ?xcreator ?creator }"

for sub_x in g.query(sub_query, initNs=dict(ns)):
print 'Sub_x %s' % sub_x

This can't be the only way to do sub_queries with SPARQL and this doesn't work well for me because the first result is on a blanknode. The stuff is really apart of rdf:seq which I haven't figured out how to get yet even though my namespace is correct. ns#_1 contains all of the Literals for Dublin Core/Exif Metadata. I'm beginning to think maybe the binary tag from ExifTool is wrong or something.