当前位置 : 主页 > 网页制作 > xml >

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

来源:互联网 收集:自由互联 发布时间:2021-06-13
这就是我想要做的: 删除类别为“none”的“span”节点. 删除“额外”节点但保留其中的文本. 删除所有“br”节点并用“p”节点替换它们 p class="normal" span class="none" extraSome text goes her
这就是我想要做的:

删除类别为“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 !~ /\A\s*\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>
网友评论