Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用Nokogiri在img标签周围添加一个图形包装器_Ruby_Nokogiri - Fatal编程技术网

Ruby 使用Nokogiri在img标签周围添加一个图形包装器

Ruby 使用Nokogiri在img标签周围添加一个图形包装器,ruby,nokogiri,Ruby,Nokogiri,我正在完成内容的迁移,需要解析大量HTML以匹配新结构 我有很多带有内联样式的图像,我需要删除这些样式,并在每个标记周围添加一个包装 这是我的方法: def convert_imgtag(html) html_body = Nokogiri::HTML::fragment(html) html_body.xpath('.//img').each do |img| **** img.xpath('.//@style').remove end

我正在完成内容的迁移,需要解析大量HTML以匹配新结构

我有很多带有内联样式的图像,我需要删除这些样式,并在每个标记周围添加一个包装

这是我的方法:

  def convert_imgtag(html)
    html_body = Nokogiri::HTML::fragment(html)
    html_body.xpath('.//img').each do |img|

      ****

      img.xpath('.//@style').remove
    end
    html_body.to_html
  end

在我添加**的地方,我需要在我的图像标签周围添加
。我尝试使用“wrap”方法,但不起作用。

我认为wrap只适用于节点集。尝试将这些更改拆分为img节点上的两次运行

html_body = Nokogiri::HTML::fragment(html)

nodes = html_body.xpath('.//img')
nodes.wrap('<figure class="center-image"> </figure>')
nodes.each do |img|
  img.xpath('.//@style').remove
end
html\u body=Nokogiri::html::fragment(html)
nodes=html_body.xpath('.//img')
nodes.wrap(“”)
每个节点都有| img|
xpath('./@style')。删除
终止