简体   繁体   English

如何使用rdflib更改rdf:description命名空间?

[英]How to change `rdf:description` namespace using rdflib?

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. 我只是尝试使用rdflib使用python管理RDF,并在此页面( http://www.obitko.com/tutorials/ontologies-semantic-web/rdf-graph-and-syntax.html )上进行了概述RDF语法。 In it, the RDF example is the following: 其中,RDF示例如下:

<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 . 在这里,RDF对象的代码表示foaf:Person ,但是在我看到的所有其他实例中,此行是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. 我很好奇这个示例是否正确,如果正确,那么此重新定义与标准rdf:Description语法之间有什么区别。 Furthermore, how would one implement this namespace in python using rdflib? 此外,如何使用rdflib在python中实现此命名空间? 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: 我目前有以下代码,该代码创建了示例RDF,但该行的结果是rdf:Description而不是foaf:Person

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

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. 具有<foo:bar>元素而不是<rdf:Description>元素是一种语法压缩,当为主题声明rdf:type时使用。

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. 这两个示例都是正确的,带有rdf:Description的版本要么缺少rdf:type三元组,要么序列化的人选择不使用特定的语法压缩。 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. 通常,这是RDF / XML的属性,有很多不同的方法来序列化同一张图,并且鉴于相同的三元组,不同的工具可能会产生不同但等效的序列化。

So in order to produce equivalent RDF/XML you would need to add an additional rdf:type triple: 因此,为了产生等效的RDF / XML,您需要添加一个附加的rdf:type三元组:

from rdflib.namespace import RDF
g.add((joe, RDF.type, FOAF.person));

Ahha! 啊哈! Found the issue, mainly thanks to RobV's response. 找到了问题,这主要归功于RobV的回应。 Its all correct, with the exception of needing the joe, RDF.type, FOAF.Person triple that RobV mentioned. 完全正确,除了需要RobV提到的joe, RDF.type, FOAF.Person三元组。 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': 但是,要使其按照示例使用的方式使用rdflib进行格式化,还必须使用“ pretty-xml”格式而不是“ 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM