加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 使用Nokogiri从元素中删除外部标记?

发布时间:2020-12-16 23:02:52 所属栏目:百科 来源:网络整理
导读:这就是我想要做的: 删除类别为“none”的“span”节点. 删除“额外”节点但保留其中的文本. 删除所有“br”节点并用“p”节点替换它们 p class="normal" span class="none" extraSome text goes here/extra /span span class="none" br/ /span span class="
这就是我想要做的:

删除类别为“none”的“span”节点.

删除“额外”节点但保留其中的文本.

删除所有“br”节点并用“p”节点替换它们

<p class="normal">
    <span class="none">
        <extra>Some text goes here</extra>
    </span>
    <span class="none">
        <br/>
    </span>
    <span class="none">
        <extra>Some other text goes here</extra>
        <br/>
    </span>
</p>

这是我想要实现的输出:

<p class="normal">Some text goes here</p>
<p class="normal">Some other text goes here</p>

到目前为止我试过这个:

doc.xpath('html/body/p/span').each do |span|
    span.attribute_nodes.each do |a|
       if a.value == "none"
          span.children.each do |child|
             span.parent << child
          end
          span.remove
       end
    end
end

但这是我得到的输出,它甚至没有正确的顺序:

<p class="normal"><br /><br />Some text goes hereSome other text goes here</p>

解决方法

试试吧

require 'rubygems'
require 'nokogiri'

doc = Nokogiri::XML(DATA)
doc.css("span.none,extra").each do |span|
  span.swap(span.children)
end

# via https://stackoverflow.com/questions/8937846/how-do-i-wrap-html-untagged-text-with-p-tag-using-nokogiri
doc.search("//br/preceding-sibling::text()|//br/following-sibling::text()").each do |node|
  if node.content !~ /As*Z/
    node.replace(doc.create_element('p',node))
  end
end

doc.css('br').remove

puts doc

__END__
<p class="normal">
    <span class="none">
        <extra>Some text goes here</extra>
    </span>
    <span class="none">
        <br/>
    </span>
    <span class="none">
        <extra>Some other text goes here</extra>
        <br/>
    </span>
</p>

哪个打印

<?xml version="1.0"?>
<p class="normal">

        <p>Some text goes here</p>





        <p>Some other text goes here</p>


</p>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读