2

The code written below gives the following output:

Code:

person = BNode()
dataStore.add((URIRef(stringrd),FOAF_NS['knows'],person))
dataStore.add((person,FOAF_NS['Person'],URIRef(fetchKnowsRowString)))
dataStore.add((person,TRUST_NS['hasValue'],Literal(trustString)))

Output:

<rdf:Description rdf:about="http://www.iamresearcher.com/profiles/id/luc.moreau">
  <foaf:knows rdf:nodeID="kdOAGjqG160"/>
</rdf:Description>

<rdf:Description rdf:nodeID="kdOAGjqG160">
  <t:data>1</t:data>
  <foaf:Person rdf:resource="http://www.iamresearcher.com/profiles/id/patrick.hayes"/>
  <foaf:Person rdf:resource="http://www.iamresearcher.com/profiles/id/christian.queinnec"/>
  <foaf:Person rdf:resource="http://www.iamresearcher.com/profiles/id/thanassis.tiropanis"/>
  <foaf:Person rdf:resource="http://www.iamresearcher.com/profiles/id/ian.foster"/>
  <foaf:Person rdf:resource="http://www.iamresearcher.com/profiles/id/nicholas.gibbins"/>
</rdf:Description>

But I need following output, could you please guide what is wrong with it.

<rdf:Description rdf:about="http://www.iamresearcher.com/profiles/id/luc.moreau">
<foaf:knows>
  <foaf:Person rdf:resource="http://www.iamresearcher.com/profiles/id/patrick.hayes">
    <t:data>1</t:data>
  </foaf:Person>
  <foaf:Person rdf:resource="http://www.iamresearcher.com/profiles/id/christian.queinnec">
    <t:data>1</t:data>
  </foaf:Person>
  <foaf:Person rdf:resource="http://www.iamresearcher.com/profiles/id/thanassis.tiropanis">
    <t:data>1</t:data>
  </foaf:Person>
  <foaf:Person rdf:resource="http://www.iamresearcher.com/profiles/id/ian.foster">
    <t:data>1</t:data>
  </foaf:Person>
  <foaf:Person rdf:resource="http://www.iamresearcher.com/profiles/id/nicholas.gibbins">
    <t:data>1</t:data>
  </foaf:Person>
</foaf:knows>
</rdf:Description>

Thanks in advance.

4

2 回答 2

0

似乎您在 bNode 上以某种方式错误地循环person。您始终使用相同的 bNode,这可能是错误的原因。

所以如果你的代码看起来像......

person = BNode()
for (fetchKnowsRowString, trustString) in friends:
   dataStore.add((URIRef(stringrd),FOAF_NS['knows'],person))
   dataStore.add((person,FOAF_NS['Person'],URIRef(fetchKnowsRowString)))
   dataStore.add((person,TRUST_NS['hasValue'],Literal(trustString)))

然后错误是您使用的是相同的 bNode 实例。您的代码应类似于下面的代码段。请注意,bNode 创建是在循环内部,这是主要区别。

for (fetchKnowsRowString, trustString) in friends:
   person = BNode()
   dataStore.add((URIRef(stringrd),FOAF_NS['knows'],person))
   dataStore.add((person,FOAF_NS['Person'],URIRef(fetchKnowsRowString)))
   dataStore.add((person,TRUST_NS['hasValue'],Literal(trustString)))
于 2011-09-16T04:05:06.790 回答
0

您的问题在这里有点含糊,首先您想要的输出实际上是无效的 RDF/XML,因此即使您想生成它也无法生成。您是否尝试过通过W3C RDF Validator运行它?它究竟是从哪里来的?

您尝试生成适合特定模式的 RDF/XML 是否有原因?

恕我直言,这是非常糟糕的做法,您真的不应该尝试这样做。
RDF 的全部意义在于它是一个基于三元组的数据模型,与数据的实际序列化是分开的。你真的不应该尝试基于所需的序列化创建 RDF,你应该创建 RDF 三元组来表达你的数据,从你展示的最小代码片段中,这似乎就是你正在做的事情。

所以我要再次重申,为什么需要以特定样式生成 RDF/XML?假设您对此有某种原因,可能会有更好的方法来实现您的实际目标,并且如果您提供更多详细信息,人们将有更好的机会能够适当地帮助您

于 2011-09-18T17:22:49.603 回答