【问题标题】:Collecting Attributes in XML using Nokogiri使用 Nokogiri 收集 XML 中的属性
【发布时间】:2012-02-16 02:28:44
【问题描述】:

我有这个 XML:

<RECEIPT receiptDate="2012-02-10T12:46:26.661Z" submissionFile="E.coli_ENT_WS.submission.xml" success="false">

  <EXPERIMENT alias="ENT 23" status="PUBLIC"/>
  <EXPERIMENT alias="WS 23" status="PUBLIC"/>
  <RUN alias="ENT 23" status="PUBLIC"/>
  <RUN alias="WS 23" status="PUBLIC"/><
  SAMPLE alias="ENT 23" status="PUBLIC"/>
  <SAMPLE alias="WS 23" status="PUBLIC"/>
  <STUDY alias="ENT 23" status="PUBLIC"/>
  <STUDY alias="WS 23" status="PUBLIC"/>
  <SUBMISSION alias="E.coli_ENT_WS"/>
  <MESSAGES>
    <ERROR> In run(ENT 23), the FC018_s_6_sequence_L70.txt.md5 not found </ERROR>
    <ERROR> In run(ENT 23) found the file format(Illumina_native_fastq), but requires SPOT_DESCRIPTOR information in the experiment(ENT 23)</ERROR>
    <ERROR>The Illumina_native_fastq file format required gzip compression for submission.</ERROR>
    <ERROR> FILE attribute quality_scoring_system is required</ERROR>
    <ERROR>Same file FC018_s_6_sequence_L70.txt found in Run(WS 23) has been used with other Run</ERROR>
    <ERROR> In run(WS 23), the FC018_s_6_sequence_L70.txt.md5 not found </ERROR>
    <ERROR> In run(WS 23) found the file format(Illumina_native_fastq), but requires SPOT_DESCRIPTOR information in the experiment(WS 23)</ERROR>
    <ERROR>The Illumina_native_fastq file format required gzip compression for submission.</ERROR>
    <ERROR> FILE attribute quality_scoring_system is required</ERROR>
    <INFO> VALIDATE action for the following XML: E.coli_ENT_WS.study.xml E.coli_ENT_WS.sample.xml E.coli_ENT_WS.experiment.xml E.coli_ENT_WS.run.xml      </INFO>
    <INFO>Inform_on_error is not filled in; auto populated from Submission account. </INFO>
    <INFO>Number of files in drop box = 2 &amp; Number of files in Submission = 1</INFO>
    <INFO>Deprecated element ignored: CENTER_NAME</INFO>
    <INFO>Deprecated element PROJECT_ID converted to RELATED_STUDY</INFO>
    <INFO>Deprecated element ignored: CENTER_NAME</INFO>
    <INFO>Deprecated element PROJECT_ID converted to RELATED_STUDY</INFO>
    <INFO> SPOT_DESCRIPTOR is missing</INFO><INFO> SPOT_DESCRIPTOR is missing</INFO>
    <INFO>Experiment (ENT 23) SPOTDESCRIPTOR is optional is null</INFO>
    <INFO>Experiment (WS 23) SPOTDESCRIPTOR is optional is null</INFO>
    <INFO> In run(ENT 23) file name (FC018_s_6_sequence_L70.txt) mentioned is not found among the submitted files</INFO>
    <INFO> In run(WS 23) file name (FC018_s_6_sequence_L70.txt) mentioned is not found among the submitted files</INFO>
  </MESSAGES>
  <ACTIONS>VALIDATE</ACTIONS>
  <ACTIONS>VALIDATE</ACTIONS>
  <ACTIONS>VALIDATE</ACTIONS>
  <ACTIONS>VALIDATE</ACTIONS>
  <ACTIONS>HOLD</ACTIONS>
</RECEIPT>

我能够检索所有元素标签,主要是EXPERIMENTERRORINFOACTIONMESSAGE

我想检索的是来自 EXPERIMENTRECEIPT 等元素的属性

我正在使用 Nokogiri 进行解析。

我的代码是这样的:

@req_test = %x[curl -F "SUBMISSION=@xml/#{@experiment.alias}.submission.xml" -F "STUDY=@xml/#{@experiment.alias}.study.xml" -F "SAMPLE=@xml/#{@experiment.alias}.sample.xml" -F "RUN=@xml/#{@experiment.alias}.run.xml" -F "EXPERIMENT=@xml/#{@experiment.alias}.experiment.xml" https://www-test.ebi.ac.uk/ena/submit/drop-box/submit/]
    @doc = Nokogiri::XML(@req_test) 

    # collecting all the errors
    @expt = @doc.xpath("//ERROR")

    # Collecting all the INFO
    @info = @doc.xpath("//INFO")

那是我的控制器。我的观点只是为了展示:

<h3>This is the ERRORS Collected</h3>
<% for expt in @expt %>
<ul>
  <li><%= expt %><br \></li>
</ul>
<% end %>

<br \ >

<h3>This is the INFO Collected</h3>

<% for info in @info %>
<ul>
  <li><%= info %><br \></li>
</ul>
<% end %>

应用程序呈现如下内容:

This is the ERRORS Collected

    In run(ENT 23), the FC018_s_6_sequence_L70.txt.md5 not found

    In run(ENT 23) found the file format(Illumina_native_fastq), but requires SPOT_DESCRIPTOR information in the experiment(ENT 23)

    The Illumina_native_fastq file format required gzip compression for submission.

    FILE attribute quality_scoring_system is required

    Same file FC018_s_6_sequence_L70.txt found in Run(WS 23) has been used with other Run

    In run(WS 23), the FC018_s_6_sequence_L70.txt.md5 not found

    In run(WS 23) found the file format(Illumina_native_fastq), but requires SPOT_DESCRIPTOR information in the experiment(WS 23)

    The Illumina_native_fastq file format required gzip compression for submission.

    FILE attribute quality_scoring_system is required


This is the INFO Collected

    VALIDATE action for the following XML: E.coli_ENT_WS.study.xml E.coli_ENT_WS.sample.xml E.coli_ENT_WS.experiment.xml E.coli_ENT_WS.run.xml

    Inform_on_error is not filled in; auto populated from Submission account.

    Number of files in drop box = 2 & Number of files in Submission = 1

    Deprecated element ignored: CENTER_NAME

    Deprecated element PROJECT_ID converted to RELATED_STUDY

    Deprecated element ignored: CENTER_NAME

    Deprecated element PROJECT_ID converted to RELATED_STUDY

    SPOT_DESCRIPTOR is missing

    SPOT_DESCRIPTOR is missing

    Experiment (ENT 23) SPOTDESCRIPTOR is optional is null

    Experiment (WS 23) SPOTDESCRIPTOR is optional is null

    In run(ENT 23) file name (FC018_s_6_sequence_L70.txt) mentioned is not found among the submitted files

    In run(WS 23) file name (FC018_s_6_sequence_L70.txt) mentioned is not found among the submitted files

请有人建议检索方法/选项。

【问题讨论】:

  • 他们在 Nokogiri 文档中说要使用 doc = Nokogiri::Slop
  • 我从网上找到的:@doc = Nokogiri::XML(File.open("myFile.xml").read) # 获取具有属性的元素:elements = @doc.xpath(" //*[@*[success]]") # 只获取属性:attributes = @doc.xpath("//@*[success]") 但仍然是空的
  • 远离 Slop。正如this document 底部的脚注所述:“不要使用这个。……不,真的,不要使用这个。如果你使用它,不要报告错误。……你一直在警告!”

标签: ruby xml nokogiri ruby-on-rails-2


【解决方案1】:

我不清楚你想做什么,或者你的问题是什么。以下是可能有帮助的各种答案。

对于任何元素,您都可以使用Nokogiri::XML::Node#attributes 获取将节点名称映射到Nokogiri::XML::Attr 的哈希值(其中有一个.value,您可以阅读):

require 'nokogiri'
require 'erb'

template = <<ENDERB
<% unless @expts.empty? %>
<h3>Experiments</h3>
<ul><% for expt in @expts %>
  <li><%= expt %><ul>
    <% expt.attributes.each do |name,attr| %>
      <li><%=name%> = <%=attr.value%></li>
    <% end %>
  </ul></li>
<% end %></ul>
<% end %>
ENDERB

doc = Nokogiri.XML(DATA)
@expts = doc.xpath("//EXPERIMENT")   
puts ERB.new(template).result(binding).gsub(/^[ \t]*\n/,'')
#=> <h3>Experiments</h3>
#=> <ul>
#=>   <li><EXPERIMENT alias="ENT 23" status="PUBLIC"/><ul>
#=>       <li>alias = ENT 23</li>
#=>       <li>status = PUBLIC</li>
#=>   </ul></li>
#=>   <li><EXPERIMENT alias="WS 23" status="PUBLIC"/><ul>
#=>       <li>alias = WS 23</li>
#=>       <li>status = PUBLIC</li>
#=>   </ul></li>
#=> </ul>

除了attributes(哈希),您还可以使用.attribute_nodes,它为您提供Attrs 的直接数组(每个.name.value)。

或者,在迭代您可以使用的实验元素时……

<%= expt['alias'] %>

…提取已知属性的值(返回字符串,例如"ENT 23")。

如果你想自己提取所有属性,你也可以使用…

@aliases = @doc.xpath('//@alias')

...如果您想在文档中的任何位置(具有.name.value)获取这些属性的数组。

如果您只想要特定元素上的所有alias 属性(例如EXPERIMENT),那么您可以使用...

@expt_aliases = @doc.xpath('//EXPERIMENT/@alias')

【讨论】:

  • 感谢 Phrogz 的回复。实际上我可以向您解释这个问题,我基本上是在尝试将一些生成的 xml 文件发送到第三方 Web 服务,该服务验证我的 xml 并返回我上面提供的 xml 格式的收据。从那个 xml 我想检索成功属性并检查它是真还是假。这告诉我我的 xml 格式是否正确。这样我可以生成一个包含错误的页面给用户重新提交他/她的数据。我希望你明白我的想法。感谢您的回复,但我让它工作了..!欢呼
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
相关资源
最近更新 更多