0

I found an example online about RDF:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#   xmlns:ns="http://www.example.org/#">
  <ns:Person rdf:about="http://www.example.org/#john">
    <ns:hasMother rdf:resource="http://www.example.org/#susan" />
    <ns:hasBrother rdf:resouce="http://www.example.org/#luke" />
  </ns:Person>
</rdf:RDF>

If John has two brothers, how would we modify the document?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
user2836163
  • 411
  • 1
  • 6
  • 16

1 Answers1

4

RDF is a graph based data representation, and what you've shown is a serialization of an RDF graph in the RDF/XML syntax. RDF/XML is not a particularly human readable serialization, and it's not great for writing by hand, either. However, in this case, you could add another brother with:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:ns="http://www.example.org/#">
  <ns:Person rdf:about="http://www.example.org/#john">
    <ns:hasBrother rdf:resource="http://www.example.org/#billy" />
    <ns:hasMother rdf:resource="http://www.example.org/#susan" />
    <ns:hasBrother rdf:resource="http://www.example.org/#luke" />
  </ns:Person>
</rdf:RDF>

The same RDF graph can be serialized in lots of different ways, though, so you can't reliably and easily manipulate RDF/XML to update a graph. For instance, the graph above can be represented as

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:ns="http://www.example.org/#" > 
  <rdf:Description rdf:about="http://www.example.org/#john">
    <rdf:type rdf:resource="http://www.example.org/#Person"/>
    <ns:hasMother rdf:resource="http://www.example.org/#susan"/>
    <ns:hasBrother rdf:resource="http://www.example.org/#billy"/>
    <ns:hasBrother rdf:resource="http://www.example.org/#luke"/>
  </rdf:Description>
</rdf:RDF>

Just like you shouldn't query RDF/XML with XPath, you shouldn't really try to modify the RDF/XML by hand (though it's not quite as bad). You should get an RDF library, load the model, modify it using the library's API, and then write it back out again.

If you do want to write it by hand, I suggest that you use the Turtle serialization, where your original graph is:

@prefix ns:    <http://www.example.org/#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

ns:john  a             ns:Person ;
        ns:hasBrother  ns:luke ;
        ns:hasMother   ns:susan .

and adding another brother is as simple as:

@prefix ns:    <http://www.example.org/#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

ns:john  a             ns:Person ;
        ns:hasBrother  ns:billy , ns:luke ;
        ns:hasMother   ns:susan .
Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thanks Joshua. Really helpful. Actually what I'm working on right now is translating a bunch of huge xml files to follow an owl schema. I'm very new to RDF/OWL and I'm writing some Java programs to utilize the XML library. Any good suggestions? – user2836163 Oct 03 '13 at 19:20
  • As to Java libraries? I tend to use [tag:jena], but there are lots out there. As for translating XML into OWL, that's a bit less pleasant, but you might find some results by [searching on SO](http://stackoverflow.com/questions/tagged/xml+owl). – Joshua Taylor Oct 03 '13 at 19:42
  • I would recommend using XSL to convert XML to RDF/XML. It is usually much better than first interpreting everything in Java and then creating the individual statements. – Rhand Oct 07 '13 at 14:27