Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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 on rails 替换rails中字符串中的img src属性值_Ruby On Rails_Xhtml - Fatal编程技术网

Ruby on rails 替换rails中字符串中的img src属性值

Ruby on rails 替换rails中字符串中的img src属性值,ruby-on-rails,xhtml,Ruby On Rails,Xhtml,我正在从xhtml文件中检索内容。内容包含带有src=“/tmp/folder\u name/file\u name”的img标记。我想将src值从“/tmp/folder\u name/file\u name”替换为“file\u name”。下面的代码是从xhtml获取内容的方法。我尝试了Nokogiri::HTML(部分内容)。但是结果内容不是在xhtml中。如何将其转换回xhtml,或者如何在没有Nokogiri::HTML的情况下替换内容中的src值 section_content

我正在从xhtml文件中检索内容。内容包含带有
src=“/tmp/folder\u name/file\u name”
的img标记。我想将src值从“/tmp/folder\u name/file\u name”替换为“file\u name”。下面的代码是从xhtml获取内容的方法。我尝试了
Nokogiri::HTML(部分内容)
。但是结果内容不是在xhtml中。如何将其转换回xhtml,或者如何在没有
Nokogiri::HTML的情况下替换内容中的src值

  section_content = section.export_xhtml_content file_path
  doc = Nokogiri::HTML(section_content)
    unless doc.css('div.image_content').blank?
      doc.css('div.image_content img').each do |img|
        newsrc = File.basename img[:src]
        img.set_attribute('src', newsrc)
      end
    end
    section_content = doc.to_s
内容:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>File 1: Chapter1</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link href="stylesheet.css" type="text/css" rel="stylesheet"/>
    <link href="page_styles.css" type="text/css" rel="stylesheet"/>
  </head>
  <body class="publitory">
    <h1 id="File_1_1">Chapter1</h1>
    <h2 id="File_1_2">Content1</h2>
    <h3 id="File_1_3">Content1.1</h3>
    <p/>
    <div style="width:25%; margin: 0 auto;" data-align="Middle" class="image_content">
       <img width="100%" src="/tmp/fog/development_publitory_bucket/uploads/user/b57030de-89ac-11e3-9cf2-bdfa8a998e1e/book/053bab68-b4b2-11e3-8ed6-996ec04a57ef/oeb_image/angel7eef59eb838ac763a43b936763dd184ec3324318.jpeg"/> 
       <div class="caption" style="clear:both;">Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content<br/></div>
   </div>
   <br/>
   <p/>
   <h3 id="File_1_4">Content1.2</h3>
   <h2 id="File_1_5">Content2</h2>
   <h2 id="File_1_6">Content3</h2>
   <h2 id="File_1_7">Content4</h2>
  </body>
</html>

文件1:第1章
第一章
内容1
内容1.1

内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容

内容1.2 内容2 内容3 内容4

使用Nokogiri替换src值后,结果内容为:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>File 1: Chapter1</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="stylesheet.css" type="text/css" rel="stylesheet">
    <link href="page_styles.css" type="text/css" rel="stylesheet">
  </head>
  <body class="publitory">
    <h1 id="File_1_1">Chapter1</h1>
    <h2 id="File_1_2">Content1</h2>
    <h3 id="File_1_3">Content1.1</h3>
    <p></p>
    <div style="width:25%; margin: 0 auto;" data-align="Middle" class="image_content">
      <img width="100%" src="angel7eef59eb838ac763a43b936763dd184ec3324318.jpeg">
      <div class="caption" style="clear:both;">Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content Content1.1 content<br>
      </div>
    </div>
    <br>
    <p></p>
    <h3 id="File_1_4">Content1.2</h3>
    <h2 id="File_1_5">Content2</h2>
    <h2 id="File_1_6">Content3</h2>
    <h2 id="File_1_7">Content4</h2>
  </body>
</html>

文件1:第1章
第一章
内容1
内容1.1

内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容1.1内容

内容1.2 内容2 内容3 内容4

结果内容应该是完美的xhtml。帮我解决这个问题。提前感谢。

您需要执行的基本步骤:

  • 构建文档,例如使用
    Nokogiri::XML
  • 使用
    .xpath
    .css
    查询查找目标节点
  • 使用提供的接口在节点上执行任何操作

  • 是的,我喜欢这样:doc=Nokogiri::HTML(section\u content),除非doc.css('div.image\u content')。空白?doc.css('div.image_content img')。每个do | img | newsrc=File.basename img[:src]img。set_attribute('src',newsrc)end-end-section_content=doc.to_那么,问题是什么?编辑的问题。请检查一下。结果内容应为xhtml格式。我尝试了regex节内容[/img.*.src=“(.*)”/I,1]。但它只给出了第一个img标签。我想要内容中的所有img标签。如果这一个有效,我的问题将得到解决。同时我更新了答案,您是否尝试过用
    XML
    构建?与使用正则表达式相比,使用XML解析文档更为可取。顺便说一句,改进问题的正确方法是编辑它们,而不是发布一个扩展的新问题。