Reputation: 421
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?
Upvotes: 0
Views: 224
Reputation: 85873
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 .
Upvotes: 4