家庭关系的基本代表

时间:2013-10-03 18:37:06

标签: rdf

我在网上找到了一个关于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>

如果John有两个兄弟,我们将如何修改文件?

1 个答案:

答案 0 :(得分:4)

RDF是基于图形的数据表示,您所展示的是RDF / XML语法中RDF图的序列化。 RDF / XML不是一个特别是人类可读的序列化,也不适合手写。但是,在这种情况下,您可以添加另一个兄弟:

<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>

但是,相同的RDF图可以通过许多不同的方式进行序列化,因此您无法可靠且轻松地操作RDF / XML来更新图形。例如,上图可以表示为

<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>

就像你shouldn't query RDF/XML with XPath一样,你不应该真的尝试手工修改RDF / XML(虽然它并不是那么糟糕)。你应该得到一个RDF库,加载模型,使用库的API修改它,然后再将它写回来。

如果您希望手动编写,我建议您使用Turtle序列化,原始图表位于:

@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 .

并添加另一个兄弟就像:

@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 .