<?xml version="1.0" encoding="iso-8859-1"?> 
<rdf:RDF
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
	xmlns:dc="http://purl.org/dc/elements/1.1/" 
	xmlns:dcterms="http://purl.org/dc/terms/" 
	xmlns:admin="http://webns.net/mvcb/"
	xmlns:thr="http://purl.org/rss/1.0/modules/threading/"
	xmlns:pb="http://www.ideaspace.net/users/wkearney/schema/postback/" 
	xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" 
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:mt="http://movabletype.org/"
	xmlns:foaf="http://xmlns.com/foaf/0.1/" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:html="http://www.w3.org/TR/REC-html40/"
	xmlns="http://purl.org/rss/1.0/"
> 

<rdf:Description rdf:about="http://www.ideaspace.net/users/wkearney/archives/entries/000405.html"> 
	<title>Parsing RDF with python</title>
	<link>http://www.ideaspace.net/users/wkearney/archives/entries/000405.html</link>
	<description>I posted my article Some RDF examples and then I asked the RDF crowd in #rdfig to chime in with...</description> 

	<dc:creator>wkearney</dc:creator> 
	<dc:date>2003-07-25T18:20:30-05:00</dc:date> 
	<dc:identifier>http://www.ideaspace.net/users/wkearney/archives/entries/000405.html</dc:identifier>
	<dc:language>en-us</dc:language>

	 
	<dc:subject>Metadata</dc:subject> 
	<dc:subject>RDF</dc:subject> 
	<dc:subject>Semantic Web</dc:subject>

	<trackback:ping rdf:resource="http://www.ideaspace.net/mt/tb.cgi/400" />
	<pb:trackbacksFrom> 
		<rdf:Seq> 
			<rdf:li rdf:resource="http://dannyayers.com/archives/001625.html" />
		</rdf:Seq>
	</pb:trackbacksFrom>

	

	
	
	
	<dcterms:abstract>I posted my article Some RDF examples and then I asked the RDF crowd in #rdfig to chime in with...</dcterms:abstract> 
	<dcterms:created>2003-07-25T18:20:30-05:00</dcterms:created> 
	<dcterms:isPartOf rdf:resource="http://www.ideaspace.net/users/wkearney/" /> 

	<mt:body><![CDATA[I posted my article <a title="Bill Kearney: Some RDF examples" href="http://www.ideaspace.net/users/wkearney/archives/entries/000404.html">Some RDF examples</a> and then I asked the RDF crowd in <a href="irc://irc.freenode.net/rdfig">#rdfig</a> to chime in with some comments.  They pointed out a syntax error or two.  But even better, Dan Connolly took the example and whipped up a quick snippet of code that reads it.  He posted it into my comment section but I've cleaned up the formatting a bit and posted it again here.]]></mt:body>
	<mt:excerpt>I posted my article Some RDF examples and then I asked the RDF crowd in #rdfig to chime in with...</mt:excerpt> 
	<mt:more><![CDATA[Firstly, make sure you've got python and the <a href="http://rdflib.net/">rdflib</a> installed and running.  Once that's done, put this snippet into a file named foafwk.py:
<pre>
# cribbed from
#  http://rdflib.net/stable/example.py

from rdflib.TripleStore import TripleStore
from rdflib.constants import TYPE
from rdflib.Namespace import Namespace
from rdflib.Literal import Literal

FOAF = Namespace("http://xmlns.com/foaf/0.1/")
EX = Namespace("http://example.org/foovocab#")

def main():
    s = TripleStore()
    s.load(",ex2.rdf")

    # find a guy named bob...
    for who in s.subjects(FOAF["name"], Literal("Bob")):
        bob = who
    
    # who is secretlyStalking bob?
    for who in s.subjects(EX["secretlyStalking"], bob):
        for n in s.objects(who, FOAF["name"]):
            print n, "is secretlyStalking Bob!"

if __name__ == '__main__':
    main()
</pre>
Then go take third example RDF and paste it into a file named ex2.rdf
<pre>
&lt;rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:eg="http://example.org/foovocab#"
  xmlns:foaf="http://xmlns.com/foaf/0.1/"&gt;
	&lt;foaf:Person rdf:nodeID="alice"&gt;
	    &lt;foaf:name&gt;Alice&lt;/foaf:name&gt;
	    &lt;foaf:knows rdf:nodeID="bob"/&gt;
	    &lt;foaf:knows rdf:nodeID="ted"/&gt;
	    &lt;eg:acquaintance rdf:nodeID="bob"/&gt;
	    &lt;eg:acquaintance rdf:nodeID="ted"/&gt;
	    &lt;eg:archNemesis rdf:nodeID="bob"/&gt;
	    &lt;eg:archNemesis rdf:nodeID="ted"/&gt;
	&lt;/foaf:Person&gt;
	&lt;foaf:Person rdf:nodeID="bob"&gt;
	    &lt;foaf:knows rdf:nodeID="alice"/&gt;
	    &lt;foaf:knows rdf:nodeID="ted"/&gt;
	    &lt;foaf:name&gt;Bob&lt;/foaf:name&gt;
	    &lt;eg:secretlyStalking rdf:nodeID="alice"/&gt;
	    &lt;eg:acquaintance rdf:nodeID="alice"/&gt;
	    &lt;eg:archNemesis rdf:nodeID="ted"/&gt;
	&lt;/foaf:Person&gt;
	&lt;foaf:Person rdf:nodeID="carol"&gt;
	    &lt;foaf:name&gt;Carol&lt;/foaf:name&gt;
	    &lt;foaf:knows rdf:nodeID="alice"/&gt;
	    &lt;foaf:knows rdf:nodeID="ted"/&gt;
	    &lt;eg:secretlyStalking rdf:nodeID="bob"/&gt;
	    &lt;eg:acquaintance rdf:nodeID="alice"/&gt;
	    &lt;eg:acquaintance rdf:nodeID="ted"/&gt;
	&lt;/foaf:Person&gt;
	&lt;foaf:Person rdf:nodeID="ted"&gt;
	    &lt;foaf:name&gt;Ted&lt;/foaf:name&gt;
	    &lt;eg:acquaintance rdf:nodeID="carol"/&gt;
	    &lt;eg:acquaintance rdf:nodeID="alice"/&gt;
	    &lt;eg:archNemesis rdf:nodeID="bob"/&gt;
	&lt;/foaf:Person&gt;
&lt;/rdf:RDF&gt;
</pre>
Now execute the program:
<pre>
$ python foafwk.py ,ex2.rdf
Carol is secretlyStalking Bob!
</pre>
You can fiddle around with the ex2.rdf file and the python example to learn what more can be done.  ]]></mt:more>
	<mt:keywords>rdf, metadata, xml, irc</mt:keywords> 
	<mt:entryID>405</mt:entryID>

	<mt:entryPrev>402</mt:entryPrev>
	<mt:entryNext>406</mt:entryNext>

	<html:link rel="prev" type="application/xml" href="http://www.ideaspace.net/users/wkearney/archives/entries/000402.html.xml" title="Talking past each other?" />
	<html:link rel="next" type="application/xml" href="http://www.ideaspace.net/users/wkearney/archives/entries/000406.html.xml" title="Giving the finger" />
	
	<mt:author>wkearney</mt:author> 
	<mt:authorNickname>Bill Kearney</mt:authorNickname> 
	<mt:authorEmail>wkearney@ideaspace.net</mt:authorEmail>
	<mt:authorURL rdf:resource="http://www.ideaspace.net/users/wkearney" /> 
	
	<foaf:name>wkearney</foaf:name> 
	<foaf:mbox rdf:resource="mailto:wkearney@ideaspace.net" /> 
	<foaf:nick>Bill Kearney</foaf:nick> 
	<foaf:homepage rdf:resource="http://www.ideaspace.net/users/wkearney" />
	
	<rdfs:seeAlso rdf:resource="http://www.ideaspace.net/users/wkearney/xml/index.rdf" />
	<admin:generatorAgent rdf:resource="http://www.movabletype.org/?v=2.64" /> 
</rdf:Description>
</rdf:RDF>