1

I'm just dabbling in managing RDF with python using rdflib, and ran across this page (http://www.obitko.com/tutorials/ontologies-semantic-web/rdf-graph-and-syntax.html) that outlines some RDF syntax. In it, the RDF example is the following:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns="http://www.example.org/~joe/contact.rdf#">
  <foaf:Person rdf:about="http://www.example.org/~joe/contact.rdf#joesmith">
    <foaf:mbox rdf:resource="mailto:joe.smith@example.org"/>
    <foaf:homepage rdf:resource="http://www.example.org/~joe/"/>
    <foaf:family_name>Smith</foaf:family_name>
    <foaf:givenname>Joe</foaf:givenname>
  </foaf:Person>
</rdf:RDF>

In here, the code for the RDF object denotes foaf:Person, but in all other instances I've seen, this line is rdf:Description. I'm curious whether this example is correct, and if so then what the difference is between this re-definition and the standard rdf:Description syntax. Furthermore, how would one implement this namespace in python using rdflib? I've currently got the following code, which creates the example RDF, but it results in rdf:Description instead of foaf:Person for that line:

import rdflib as rd
from rdflib.namespace import FOAF

joe = rd.URIRef("http://www.example.org/~joe/contact.rdf#joesmith")
joeEmail = rd.URIRef('mailto:joe.smith@example.org')
joeHome = rd.URIRef('http://www.example.org/~joe/')
joeFamily = rd.Literal('Smith')
joeName = rd.Literal('Joe')

g = rd.Graph()

g.bind('foaf', FOAF, False)

g.add((joe, FOAF.mbox, joeEmail))
g.add((joe, FOAF.homepage, joeHome))
g.add((joe, FOAF.family_name, joeFamily))
g.add((joe, FOAF.givenname, joeName))

fp = open('outfile.rdf','wb')
fp.write(g.serialize(format='xml'))
fp.close()
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Topher
  • 498
  • 5
  • 17
  • 1
    The example is legal RDF/XML. as RobV notes in an answer, when you have a triple `x rdf:type ex:Foo`, you can replace `...` with `...`. For that specific issue, you may find the question [Why some rdf files does not contain ?](http://stackoverflow.com/q/18204160/1281433) relevant. – Joshua Taylor Aug 25 '15 at 12:05
  • 1
    'pretty-xml' was [the solution](http://stackoverflow.com/a/24046718/1281433) to [Using owl:Class prefix with rdflib and xml serialization](http://stackoverflow.com/q/24017320/1281433), where a user had a triple of the form "x rdf:type owl:Class" and wanted to get ` ...` as the output. Maybe this could be considered a duplicate? – Joshua Taylor Aug 26 '15 at 14:48

2 Answers2

2

Having a <foo:bar> element as opposed to a <rdf:Description> element is a syntactic compression that is used when a rdf:type is declared for a subject.

Both examples are correct the version with rdf:Description is either missing the rdf:type triple or the serialised has chosen not to use the specific syntactic compression. In general this is a property of RDF/XML, there are lots of different ways to serialise the same graph and given the same set of triples different tools may produce different but equivalent serialisations.

So in order to produce equivalent RDF/XML you would need to add an additional rdf:type triple:

from rdflib.namespace import RDF
g.add((joe, RDF.type, FOAF.person));
RobV
  • 28,022
  • 11
  • 77
  • 119
  • 1
    There's more about this particular abbreviation in [Why some rdf files does not contain ?](http://stackoverflow.com/q/18204160/1281433) – Joshua Taylor Aug 25 '15 at 12:06
  • Thanks! Yeah I knew its an rdf:type declaration, but in attempts to declare and serialize it with rdflib it was just showing up as a triple within the description, but i guess thats just how its being serialized. – Topher Aug 25 '15 at 12:06
1

Ahha! Found the issue, mainly thanks to RobV's response. Its all correct, with the exception of needing the joe, RDF.type, FOAF.Person triple that RobV mentioned. However, to get it to format using rdflib in the way the example uses, one has to also serialize it using the 'pretty-xml' format, as opposed to just 'xml':

...
g.add((joe, RDF.type, FOAF.Person)) # <---- HERE...
g.add((joe, FOAF.mbox, joeEmail))
g.add((joe, FOAF.homepage, joeHome))
g.add((joe, FOAF.family_name, joeFamily))
g.add((joe, FOAF.givenname, joeName))

fp = open('RDF/outfile.rdf','wb')
fp.write(g.serialize(format='pretty-xml'))  # <---- ...AND HERE
fp.close()
Topher
  • 498
  • 5
  • 17
  • 'pretty-xml' was [the solution](http://stackoverflow.com/a/24046718/1281433) to [Using owl:Class prefix with rdflib and xml serialization](http://stackoverflow.com/q/24017320/1281433), too. – Joshua Taylor Aug 26 '15 at 14:46