0

私の目標:件名のxmlドキュメントで「SECTION」という名前のすべての要素を取得したいと思います。各セクションとその下のすべてを取得します。

制約:LibXMLRubyを使用する必要があります。つまり、「xml」が必要です。

問題:出力データが切り捨てられます。

質問(出力file1.xmlを参照)

  • file1.xmlの出力が切り捨てられるのはなぜですか?注:最初のP(a)... / Pタグの間のテキストの多く(注:切り捨ては「倫理...」という単語で始まります)
  • コードが最後の2つのP要素(P(b)...、P(2)...)とCITA要素を削除したのはなぜですか?何が原因ですか?xml version = "1.0" encoding = "UTF-8"?およびSECTION/を出力の最後に表示しますか?

注:出力file2.xmlには、さらに厳しい切り捨てがあります。それが何かを明らかにする場合に備えて、私はそれを含めています。

コードは次のとおりです。

#!/usr/bin/ruby
require "xml"
reader = XML::Reader.file('infile2.xml')
while reader.read
  node = reader.node 
    if node.name == "SECTION"
      iteration = XML::Document.string(node.to_s)
      puts iteration
      puts "\n"
    end
end

入力file1.xml:

<?xml version="1.0"?>
<SECTION>
  <SECTNO>§ 0.735-1</SECTNO>
  <SUBJECT>Agency ethics officials.</SUBJECT>
  <P>(a) <E T="03">Designated Agency Ethics Official (DAEO).</E> The Assistant General Counsel (023) is the designated agency ethics official (DAEO) for the Department of Veterans Affairs. The Deputy Assistant General Counsel (023C) is the alternate DAEO, who is designated to act in the DAEO's absence. The DAEO has primary responsibility for the administration, coordination, and management of the VA ethics program, pursuant to 5 CFR 2638.201-204.</P>
  <P>(b) <E T="03">Deputy ethics officials.</E> (1) The Regional Counsel are deputy ethics officials. They have been delegated the authority to act for the DAEO within their jurisdiction, under the DAEO's supervision, pursuant to 5 CFR 2638.204.</P>
  <P>(2) The alternate DAEO, the DAEO's staff, and staff in the Offices of Regional Counsel, may also act as deputy ethics officials pursuant to delegations of one or more of the DAEO's duties from the DAEO or the Regional Counsel.</P>
  <CITA>[58 FR 61813, Nov. 23, 1993. Redesignated at 61 FR 11309, Mar. 20, 1996]</CITA>
</SECTION>

入力file1.xml(上記)を指定して出力:

<?xml version="1.0" encoding="UTF-8"?>
<SECTION>
  <SECTNO>§ 0.735-1</SECTNO>
  <SUBJECT>Agency ethics officials.</SUBJECT>
  <P>(a) <E T="03">Designated Agency Ethics Official (DAEO).</E> The Assistant General Counsel (023) is the designated agency ethics official (DAEO) for the Department of Veterans Affairs. The Deputy Assistant General Counsel (023C) is the alternate DAEO, who is designated to act in the DAEO's absence. The DAEO has primary responsibility for the administration, coordination, and management of the VA ethic</P></SECTION>

<?xml version="1.0" encoding="UTF-8"?>
<SECTION/>

入力file2.xml:

<?xml version="1.0"?>
<SUBPART>
  <HD SOURCE="HED">Subpart A—General Provisions</HD>
  <SECTION>
    <SECTNO>§ 0.735-1</SECTNO>
    <SUBJECT>Agency ethics officials.</SUBJECT>
    <P>(a) <E T="03">Designated Agency Ethics Official (DAEO).</E> The Assistant General Counsel (023) is the designated agency ethics official (DAEO) for the Department of Veterans Affairs. The Deputy Assistant General Counsel (023C) is the alternate DAEO, who is designated to act in the DAEO's absence. The DAEO has primary responsibility for the administration, coordination, and management of the VA ethics program, pursuant to 5 CFR 2638.201-204.</P>
    <P>(b) <E T="03">Deputy ethics officials.</E> (1) The Regional Counsel are deputy ethics officials. They have been delegated the authority to act for the DAEO within their jurisdiction, under the DAEO's supervision, pursuant to 5 CFR 2638.204.</P>
    <P>(2) The alternate DAEO, the DAEO's staff, and staff in the Offices of Regional Counsel, may also act as deputy ethics officials pursuant to delegations of one or more of the DAEO's duties from the DAEO or the Regional Counsel.</P>
    <CITA>[58 FR 61813, Nov. 23, 1993. Redesignated at 61 FR 11309, Mar. 20, 1996]</CITA>
  </SECTION>
  <SECTION>
    <SECTNO>§ 0.735-2</SECTNO>
    <SUBJECT>Government-wide standards.</SUBJECT>
    <P>For government-wide standards of ethical conduct and related responsibilities for Federal employees, see 5 CFR Part 735 and Chapter XVI.</P>
    <CITA>[61 FR 11309, Mar. 20, 1996. Redesignated at 63 FR 33579, June 19, 1998]</CITA>
  </SECTION>
</SUBPART>

入力file2.xml(上記)を指定して出力:

<?xml version="1.0" encoding="UTF-8"?>
<SECTION>
    <SECTNO>§ 0.735-1</SECTNO>
    <SUBJECT>Agency ethics officials.</SUBJECT>
    <P>(a) <E T="03">Designated Agency Ethics Official (DAEO).</E></P></SECTION>

<?xml version="1.0" encoding="UTF-8"?>
<SECTION/>

<?xml version="1.0" encoding="UTF-8"?>
<SECTION>
    <SECTNO>§ 0.735-2</SECTNO>
    <SUBJECT>Government-wide standards.</SUBJECT>
    <P>For government-wide standards of ethical conduct and related responsibilities for Federal employees, see 5 CFR Part 735 and Chapter XVI.</P>
    <CITA/></SECTION>

<?xml version="1.0" encoding="UTF-8"?>
<SECTION/>
4

1 に答える 1

0

巨大な XML ドキュメントがない限り、代わりに次のようなものを検討してください。

require "xml"
doc = XML::Document.file('infile1.xml')
doc.find('/SECTION').each do |s|
  puts "[#{s}]"
end

これは以下を出力します:

<SECTION>
  <SECTNO>§ 0.735-1</SECTNO>
  <SUBJECT>Agency ethics officials.</SUBJECT>
  <P>(a) <E T="03">Designated Agency Ethics Official (DAEO).</E> The Assistant General Counsel (023) is the designated agency ethics official (DAEO) for the Department of Veterans Affairs. The Deputy Assistant General Counsel (023C) is the alternate DAEO, who is designated to act in the DAEO's absence. The DAEO has primary responsibility for the administration, coordination, and management of the VA ethics program, pursuant to 5 CFR 2638.201-204.</P>
  <P>(b) <E T="03">Deputy ethics officials.</E> (1) The Regional Counsel are deputy ethics officials. They have been delegated the authority to act for the DAEO within their jurisdiction, under the DAEO's supervision, pursuant to 5 CFR 2638.204.</P>
  <P>(2) The alternate DAEO, the DAEO's staff, and staff in the Offices of Regional Counsel, may also act as deputy ethics officials pursuant to delegations of one or more of the DAEO's duties from the DAEO or the Regional Counsel.</P>
  <CITA>[58 FR 61813, Nov. 23, 1993. Redesignated at 61 FR 11309, Mar. 20, 1996]</CITA>
</SECTION>

これは質問への回答ではなく、回避策です。

リーダーを使用する際の実際の問題が何であるかはわかりませんが、カーソルっぽいことに関係があると思われます。たとえば、次のように動作します。最初の XML ドキュメントにはまだ空のセクションが追加されています。

if node.name == "SECTION"
  puts "#{reader.read_outer_xml}"
end
于 2013-03-08T20:38:47.897 に答える