替换 Nokogiri 节点中的部分文本,同时保留内容中的标记

问题描述 投票:0回答:2

我试图通过使用 Nokogiri 扫描节点的内容,然后执行

gsub
来替换一堆文件中唯一字符串的实例。我将字符串的一部分保留在适当的位置,并将其转换为锚标记。然而,大多数节点的内容都有各种形式的标记,而不仅仅是简单的字符串。例如,假设我有一个这样的文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<html>
    <head>
        <title>Title</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div>
            <p class="header">&lt;&lt;2&gt;&gt;Header</p>
            <p class="paragraph">
            <p class="text_style">Lorem ipsum blah blah blah. &lt;&lt;3&gt;&gt; Here is more content. <span class="style">Preserve this.</span> Blah blah extra text.</p>
        </div>
    </body>
</html>

整个文档中都有数字,周围有

&lt;&lt;
&gt;&gt;
。我想获取数字的值并将其转换为这样的标签:
<a id='[#]'/>
,但我想保留同一部分中其他元素的 HTML 标记,即
<span class="style">Preserve this.</span>

这是我尝试过的一切:

file = File.open("file.xhtml") {|f| Nokogiri::XML(f)}

file.xpath("//text()").each { |node|
    if node.text.match(/<<([^_]*)>>/)
        new_content = node.text.gsub(/<<([^_]*)>>/,"<a id=\"\\1\"/>")
        node.parent.inner_html = new_content
    end
}

gsub
工作正常,但由于它使用
.text
方法,因此任何标记都会被忽略并被有效清除。在这种情况下,
<span class="style">Preserve this.</span>
部分被完全删除。 (仅供参考,我使用
.parent
方法,因为如果我只是这样做
node.inner_html = new_content
我会收到此错误:
add_child_node': cannot reparent Nokogiri::XML::Element there (ArgumentError)
。)

如果我这样做:

    new_content = node.text.gsub(/<<([^_]*)>>/,"<a id=\"\\1\"/>")
    node.content = new_content

字符未正确转义:文件以

&lt;a id="3"/&gt;
而不是
<a id="3"/>
结尾。

我尝试使用 CSS 方法,如下所示:

file.xpath("*").each { |node|
    if node.inner_html.match(/&lt;&lt;([^_]*)&gt;&gt;/)
        new_content = node.inner_html.gsub(/&lt;&lt;([^_]*)&gt;&gt;/,"<a id=\"\\1\"/>")
        node.inner_html = new_content
    end
}

gsub
有效,标记被保留,并且替换的标签被正确转义。但是
<head>
<body>
标签被删除,这会导致文件无效:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Title</title>
        <link href="style.css" rel="stylesheet" type="text/css"/>
        <div>
            <p class="header"><a id="2"/>Header</p>
            <p class="paragraph">
            </p><p class="text_style">Lorem ipsum blah blah blah. <a id="3"/> Here is more content. <span class="style">Preserve this.</span> Blah blah extra text. </p>    
    </div>
</html>

我怀疑这与我迭代所有节点(

file.css("*")
)有关,这也是多余的,因为除了子节点之外,还会扫描父节点。

我已经在网上搜索过,但找不到任何解决方案。我只是希望能够交换唯一的文本,同时维护标记并使其正确编码。我在这里遗漏了一些非常明显的东西吗?

ruby nokogiri
2个回答
3
投票

看起来效果很好:

require 'nokogiri'

doc = Nokogiri::XML(<<EOT)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<html>
    <head>
        <title>Title</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div>
            <p class="header">&lt;&lt;2&gt;&gt;Header</p>
            <p class="paragraph">
            <p class="text_style">Lorem ipsum. &lt;&lt;3&gt;&gt; more content. <span class="style">Preserve this.</span> extra text.</p>
        </div>
    </body>
</html>
EOT

doc.search("//text()[contains(.,'<<')]").each do |node|
  node.replace(node.content.gsub(/<<(\d+)>>/, '<a id="[\1]" />'))
end

结果是:

puts doc.to_html

# >> <html>
# >>     <head>
# >> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
# >>         <title>Title</title>
# >>         <link href="style.css" rel="stylesheet" type="text/css">
# >>     </head>
# >>     <body>
# >>         <div>
# >>             <p class="header"><a id="[2]"></a>Header</p>
# >>             <p class="paragraph">
# >>             <p class="text_style">Lorem ipsum. <a id="[3]"></a> more content. <span class="style">Preserve this.</span> extra text.</p>
# >>         </p>
# >>     </div>
# >> </body>
# >> </html>

Nokogiri 正在添加

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

行,可能是因为标记被定义为 XML。

选择器

"//text()[contains(.,'<<')]"
仅查找包含
'<<'
的文本节点。如果可能导致误报,您可能需要对其进行修改以使其更加具体。有关语法,请参阅“XPath:在 contains 函数中使用正则表达式”。

replace
正在表演魔术;您试图修改 Nokogiri::XML::Text 节点以包含
<a.../>
,但不能,
<
>
必须进行编码。将节点更改为 Nokogiri::XML::Element(Nokogiri 默认为
<a id="[2]">
),让它按照您想要的方式存储它。


0
投票

就我而言,我通过记录课程发现了一个

Nokogiri::XML::Element

puts content.class

为了在不改变 HTML/XML 结构的情况下更改文本,您必须使用递归函数向下钻取到文档的叶子。

  def replace_text(content)
    if content.children.any?
      content.children.each do |node|
        replace_text(node)
      end
    elsif content.text
      content.content = content.text.gsub(/REGEX/, "REPLACE_VALUE")
    end
  end

用途:

content = document.css('.myclass')

return replace_text(content)
© www.soinside.com 2019 - 2024. All rights reserved.