2011-09-15 31 views
2

添加空白節點下面寫的代碼提供了以下的輸出:在三元

代碼:

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

輸出:

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

但我需要下面的輸出,可以請你指導什麼它是錯的。

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

在此先感謝。

回答

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

那麼錯誤是,您使用的是相同的B節點的實例。你的代碼應該看起來像下面的代碼片段。請注意,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))) 
0

你的問題有點模糊這裏,一開始您所需的輸出實際上是無效的RDF/XML,所以你能不能產生它,即使你想。你甚至嘗試通過W3C RDF Validator運行它,它究竟是從哪裏來的?

您是否有理由試圖生成符合特定模式的RDF/XML?

恕我直言,這是非常糟糕的做法,你真的不應該試圖做到這一點。
RDF的重點在於它是一個基於三元組的數據模型,它與數據的實際序列化是分開的。你絕對不應該嘗試根據期望的序列化來創建RDF,你應該創建RDF三元組來表達你的數據,這些數據從你展示的最小代碼片段看起來似乎是你正在做的。

所以我再次重申,爲什麼你需要以特定的風格生成RDF/XML?假設你有一些理由,可能有更好的方法來實現你的實際目標,如果你提供更多的細節,人們將有更好的機會能夠適當地幫助你