我改变一个XML表格导入到某个HTML表格,并有做节点的一些节点重新安排。显示引入nokogiri儿童为原料HTML而不是>标签<
为了实现转型,我刮XML,把它变成一个二维数组,然后建立新的HTML输出。
但是一些单元格中有HTML标签,并且在我的转换<su>
变成>su<
后。
的XML数据是:
<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>
我正在将其转换为HTML表的步骤是:
class TableCell
attr_accessor :text, :rowspan, :colspan
def initialize(text='')
@text = text
@rowspan = 1
@colspan = 1
end
end
@frag = Nokogiri::HTML(xml)
# make a 2d array to store how the cells should be arranged
column = 0
prev_row = -1
@frag.xpath("boxhd/ched").each do |ched|
row = ched.xpath("@h").first.value.to_i - 1
if row <= prev_row
column +=1
end
prev_row = row
@data[row][column] = TableCell.new(ched.inner_html)
end
# methods to find colspan and rowspan, put them in @data
# ... snip ...
# now build an html table
doc = Nokogiri::HTML::DocumentFragment.parse ""
Nokogiri::HTML::Builder.with(doc) do |html|
html.table {
@data.each do |tr|
html.tr {
tr.each do |th|
next if th.nil?
html.th(:rowspan => th.rowspan, :colspan => th.colspan).table_header th.text
end
}
end
}
end
这给出了以下的HTML(注意标被转义):
<table>
<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>
</table>
如何获取原始HTML而不是实体?
我试着这些没有成功
@data[row][column] = TableCell.new(ched.children)
@data[row][column] = TableCell.new(ched.children.to_s)
@data[row][column] = TableCell.new(ched.to_s)