Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:29:12+00:00 2026-05-31T02:29:12+00:00

This is what I want to do: Remove span nodes with a class of

  • 0

This is what I want to do:

Remove “span” nodes with a class of “none”.

Remove “extra” nodes but keep the text inside them.

Remove any “br” nodes and replace them with “p” nodes

<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>

This is the output I’d like to achieve:

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

I’ve tried this so far:

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

But this is the output I’m getting, it’s not even in the right order:

<p class="normal"><br /><br />Some text goes hereSome other text goes here</p>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:29:13+00:00Added an answer on May 31, 2026 at 2:29 am

    Try this out

    require 'rubygems'
    require 'nokogiri'
    
    doc = Nokogiri::XML(DATA)
    doc.css("span.none, extra").each do |span|
      span.swap(span.children)
    end
    
    # via http://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>
    

    Which prints

    <?xml version="1.0"?>
    <p class="normal">
    
            <p>Some text goes here</p>
    
    
    
    
    
            <p>Some other text goes here</p>
    
    
    </p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple span like so <span class=action removeAction>Remove</span> This span is within
I have this html right now: <h1><span class = 'highlight'><input type='image' class='removeall' src='remove.png'> <input
I want remove "Language" querystring from my url. How can I do this? (using
I want to remove a cookie I set. If I do this by giving
I have this matrix of size 10000 by 1. I want to remove some
I have a string like this A. rahul VyAs and i want to remove
if i remove a.three display block this shows up fine. However what i want
I have a span inside a legend tag that I want to use to
i'm using UIWebview to display a local html page. i tried this text<span><span/>text in
lets say i have this lump of HTML: <div class=container> <span class=title>Heading 1</span> This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.