2011-03-02 48 views
2

我是RDF的新手,但以下看起來很瑣碎;我只是沒有得到我期待的結果。我有一個RDF/XML文件,如下所示(它在W3C驗證驗證):用rdf gem解析RDF(RDFXML)ruby - 不按預期工作

<?xml version="1.0" encoding="utf-8"?> 
<rdf:RDF 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:ore="http://blah.com" 
    xmlns:dms="http://blah.com" 
> 
    <ore:Aggregation rdf:about="http://blah.com/data/Manifest"> 
    <ore:aggregates> 
     <ore:Aggregation rdf:about="http://blah.com/data/something"> 
     <rdf:type rdf:resource="http://blah.com/data/Sequence"/> 
     <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#List"/> 
     </ore:Aggregation> 
    </ore:aggregates> 
    <ore:aggregates> 
     <ore:Aggregation rdf:about="http://blah.com/data/somethingElse"> 
     <rdf:type rdf:resource="http://blah.com/data/Sequence"/> 
     <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#List"/> 
     </ore:Aggregation> 
    </ore:aggregates> 
    </ore:Aggregation> 
</rdf:RDF> 

我嘗試使用RDF寶石如下解析它,但它不是打印出報表/三:

reader.to_yaml的
RDF::RDFXML::Reader.open(image_collection_file) do |reader| 
    puts reader.to_yaml 
    puts reader.inspect 
    reader.each_statement do |statement| 
    statement.inspect! # nothing here 
    end 
end 

輸出是:

--- !ruby/object:RDF::TriX::Reader 
implementation: !ruby/object:Module {} 

input: !ruby/object:File {} 

library: :nokogiri 
options: 
    :validate: false 
    :intern: true 
    :canonicalize: false 
    :prefixes: {} 

xml: !ruby/object:Nokogiri::XML::Document 
    decorators: 
    errors: [] 

    node_cache: [] 

reader.inspect的輸出是:

#<RDF::TriX::Reader:0x101832a50 @implementation=RDF::TriX::Reader::Nokogiri, @xml=#<Nokogiri::XML::Document:0x80c16cd8 name="document" children=[#<Nokogiri::XML::Element:0x80c14b7c name="RDF" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> children=[#<Nokogiri::XML::Text:0x80c14690 "\n ">, #<Nokogiri::XML::Element:0x80c14640 name="Aggregation" namespace=#<Nokogiri::XML::Namespace:0x80c14528 prefix="ore" href="http://blah.com"> attributes=[#<Nokogiri::XML::Attr:0x80c144c4 name="about" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> value="http://blah.com/data/Manifest">] children=[#<Nokogiri::XML::Text:0x80c13bdc "\n ">, #<Nokogiri::XML::Element:0x80c13b78 name="aggregates" namespace=#<Nokogiri::XML::Namespace:0x80c14528 prefix="ore" href="http://blah.com"> children=[#<Nokogiri::XML::Text:0x80c13628 "\n  ">, #<Nokogiri::XML::Element:0x80c135c4 name="Aggregation" namespace=#<Nokogiri::XML::Namespace:0x80c14528 prefix="ore" href="http://blah.com"> attributes=[#<Nokogiri::XML::Attr:0x80c13470 name="about" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> value="http://blah.com/data/something">] children=[#<Nokogiri::XML::Text:0x80c12b88 "\n  ">, #<Nokogiri::XML::Element:0x80c12b24 name="type" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> attributes=[#<Nokogiri::XML::Attr:0x80c129bc name="resource" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> value="http://blah.com/data/Sequence">]>, #<Nokogiri::XML::Text:0x80c12a5c "\n  ">, #<Nokogiri::XML::Element:0x80c12160 name="type" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> attributes=[#<Nokogiri::XML::Attr:0x80c11ff8 name="resource" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> value="http://www.w3.org/1999/02/22-rdf-syntax-ns#List">]>, #<Nokogiri::XML::Text:0x80c12084 "\n  ">]>, #<Nokogiri::XML::Text:0x80c1165c "\n ">]>, #<Nokogiri::XML::Text:0x80c114e0 "\n ">, #<Nokogiri::XML::Element:0x80c11490 name="aggregates" namespace=#<Nokogiri::XML::Namespace:0x80c14528 prefix="ore" href="http://blah.com"> children=[#<Nokogiri::XML::Text:0x80c10f2c "\n  ">, #<Nokogiri::XML::Element:0x80c10edc name="Aggregation" namespace=#<Nokogiri::XML::Namespace:0x80c14528 prefix="ore" href="http://blah.com"> attributes=[#<Nokogiri::XML::Attr:0x80c10d74 name="about" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> value="http://blah.com/data/somethingElse">] children=[#<Nokogiri::XML::Text:0x80c1048c "\n  ">, #<Nokogiri::XML::Element:0x80c1043c name="type" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> attributes=[#<Nokogiri::XML::Attr:0x80c102d4 name="resource" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> value="http://blah.com/data/Sequence">]>, #<Nokogiri::XML::Text:0x80c10374 "\n  ">, #<Nokogiri::XML::Element:0x80c0fa64 name="type" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> attributes=[#<Nokogiri::XML::Attr:0x80c0f910 name="resource" namespace=#<Nokogiri::XML::Namespace:0x80c14af0 prefix="rdf" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> value="http://www.w3.org/1999/02/22-rdf-syntax-ns#List">]>, #<Nokogiri::XML::Text:0x80c0f99c "\n  ">]>, #<Nokogiri::XML::Text:0x80c0ef74 "\n ">]>, #<Nokogiri::XML::Text:0x80c0edf8 "\n ">]>, #<Nokogiri::XML::Text:0x80c0ec7c "\n">]>]>, @options={:validate=>false, :intern=>true, :canonicalize=>false, :prefixes=>{}}, @library=:nokogiri, @input=#<File:data/test.xml>> 

這是預期的行爲? statement.to_yaml是空的。我只想獲取RDF中的數據(此示例已簡化)。有沒有人有任何意見/見解/網址分享?

乾杯, 凡妮莎

+0

您可能還喜歡在semanticoverflow.com上問你的問題,因爲你更有可能讓人們看到常規使用語義網和RDF.rb等庫的人 – RobV 2011-03-03 10:36:54

回答

1

我不知道你爲什麼這樣做的reader.to_yaml電話或什麼它的目的是因爲我不是一個Ruby程序員。

但是看着您試圖輸出語句的代碼的關鍵位,您似乎錯過了puts關鍵字。該documentationRDF::RDFXML在GitHub上有下面的例子:

RDF::RDFXML::Reader.open("./etc/doap.xml") do |reader| 
    reader.each_statement do |statement| 
    puts statement.inspect 
    end 
end 

在你的榜樣你statement.inspect之前錯過了puts關鍵字 - 嘗試添加這回,看看是否能工程