2015-10-23 113 views
0

我的任務是將XML錶轉換爲HTML表。問題是XML不遵循HTML約定,我將不得不將節點移動到正確的位置。標題是預先排序的,而不是按層排序的,並且最後一個表格行和結束表格標記之間有表格筆記。Nokogiri將節點移動到父節點的兄弟

我通過使用構建器計算和創建HTML,然後用生成的HTML替換XML表頭來解決預訂順序轉換問題。但最後一個問題應該很簡單,給了我一個精神上的打擊。我需要將<TNOTE>移出<GPOTABLE>,並在</GPOTABLE>之後立即將它放入<div>

XML數據片段是:

<P>(vi) Grinding wheels or discs for vertical single-spindle disc grinders shall be encircled with hoods to remove the dust generated in the operation. The hoods shall be connected to one or more branch pipes having exhaust volumes as shown in Table D-57.5.</P> 
<GPOTABLE CDEF="s15,6,6,6,6" COLS="5" OPTS="L2"> 
    <TTITLE>Table D-57.5—Vertical Spindle Disc Grinder</TTITLE> 
    <BOXHD> 
    <CHED H="1">Disc diameter, inches (cm)</CHED> 
    <CHED H="1">One-half or more of disc covered</CHED> 
    <CHED H="2">Number <SU>1</SU> 
    </CHED> 
    <CHED H="2">Exhaust foot <SU>3</SU>/min.</CHED> 
    <CHED H="1">Disc not covered</CHED> 
    <CHED H="2">Number <SU>1</SU> 
    </CHED> 
    <CHED H="2">Exhaust foot<SU>3</SU>/min.</CHED> 
    </BOXHD> 
    <ROW> 
    <ENT I="01">Up to 20 (50.8)</ENT> 
    <ENT>1</ENT> 
    <ENT>500</ENT> 
    <ENT>2</ENT> 
    <ENT>780</ENT> 
    </ROW> 

    <!-- ....snip .... --> 

    <ROW> 
    <ENT I="01">Over 53 to 72 (134.62 to 182.88)</ENT> 
    <ENT>2</ENT> 
    <ENT>3,140</ENT> 
    <ENT>5</ENT> 
    <ENT>6,010</ENT> 
    </ROW> 
    <TNOTE> 
    <SU>1</SU> Number of exhaust outlets around periphery of hood, or equal distribution provided by other means.</TNOTE> 
</GPOTABLE> 
<P>(vii) Grinding and polishing belts shall be provided with hoods to remove dust and dirt generated in the operations and the hoods shall be connected to branch pipes having exhaust volumes as shown in Table D-57.6.</P> 

轉換爲HTML後,它應該是這個樣子:

<table cdef="s15,6,6,6,6" cols="5" opts="L2"> 
    <caption>Table D-57.5—Vertical Spindle Disc Grinder</caption> 
    <tr> 
     <th rowspan="2" colspan="1" class="table_header">Disc diameter, inches (cm)</th> 
     <th rowspan="1" colspan="2" class="table_header">One-half or more of disc covered</th> 
     <th rowspan="1" colspan="2" class="table_header">Disc not covered</th> 
    </tr> 
    <tr> 
     <th rowspan="1" colspan="1" class="table_header">Number <su>1</su></th> 
     <th rowspan="1" colspan="1" class="table_header">Exhaust foot <su>3</su>/min.</th> 
     <th rowspan="1" colspan="1" class="table_header">Number <su>1</su> </th> 
     <th rowspan="1" colspan="1" class="table_header">Exhaust foot<su>3</su>/min.</th> 
    </tr> 
    <tr> 
     <td i="01">Up to 20 (50.8)</td> 
     <td>1</td> 
     <td>500</td> 
     <td>2</td> 
     <td>780</td> 
    </tr> 

    <!-- .... snip .... --> 
    <tr> 
     <td i="01">Over 53 to 72 (134.62 to 182.88)</td> 
     <td>2</td> 
     <td>3,140</td> 
     <td>5</td> 
     <td>6,010</td> 
    </tr> 
    </table> 
    <div class='tnote'><su>1</su> Number of exhaust outlets around periphery of hood, or equal distribution provided by other means</div> 

這裏是我到目前爲止有:

def xslt_tables(xml_text) 

    frag = Nokogiri::HTML(xml_text) 

    frag.xpath("//gpotable").each do |table| 

     TableConverter.new(table) 
     table.name = 'table' 

    end 

    frag.inner_html 

end 
class TableConverter 

    attr_accessor :data, :rows, :columns, :frag 

    # Expects a nokogiri object (a single <gpotable> node), not merely an html fragment 

    def initialize(nokogiri_fragment) 

    @column_index = 0 
    @frag = nokogiri_fragment 

    puts "find table size..." 
    find_table_size() 

    puts "populating the grid..." 
    populate_grid() 

    puts "computing rowspans and colspans, save in @data..." 
    compute_rowspans_and_colspans() 

    puts "assemble headers from @data" 
    nokogiri_headers = html_headers() 

    puts "replace the boxhd with nokogiri_headers, translate remaining table entities"  
    replace_nodes(nokogiri_headers) 

    end 

# .... snip .... 

    def replace_nodes(headers) 

    # note: this actually changes values in the original nokogiri object! 
    # I'll leave it to the calling script to change the name to <table> 
# @frag.xpath("//gpotable").each do |table| 
#  puts "renaming //gpotable" 
#  table.name = 'table' 
# end 

    @frag.xpath("ttitle").each do |cap| 
     puts "replacing ttitle with caption" 
     cap.name = 'caption' 
    end 

    @frag.xpath("boxhd").each do |old| 
     puts "replacing boxhd with generated th with computed rowspan and colspan" 
     old.replace headers 
    end 

    @frag.xpath("row").each do |row| 
     puts "renaming row to tr" 
     row.name = 'tr' 
    end  

    @frag.xpath("tr/ent").each do |ent| 
     puts "renaming ent to td" 
     ent.name = 'td' 
    end 

    @frag.xpath("tnote").each do |tfoot| 
     puts "moving tnote" 
     tfoot.add_next_sibling('tnote') 
    end 

    end 

end 

顯然,與TnOTE位置的最後一個塊是錯誤的,但我難倒如何釘節點(S)上的@frag結束。

我會在正確的方向上沒有任何碰了一下感謝; Nokogiri教程和作弊表對我來說沒有任何意義。發佈後

回答

0

三個小時,很明顯的(現在我看到它)回答嫌我的額頭......

@frag.xpath("tnote").each do |tfoot| 
     puts "moving tnote" 
     tfoot.parent.add_next_sibling(tfoot).name = 'div' 
    end 

希望這可以幫助其他人。

相關問題