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 150595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:18:56+00:00 2026-05-11T09:18:56+00:00

I’d like to figure out a way to get to the HTML result (mentioned

  • 0

I’d like to figure out a way to get to the HTML result (mentioned further below) by using the following Ruby code and Nokogiri:

require 'rubygems' require 'nokogiri'  value = Nokogiri::HTML.parse(<<-HTML_END)   '<html>     <body>       <p id='1'>A</p>       <p id='2'>B</p>       <h1>Bla</h1>       <p id='3'>C</p>       <p id='4'>D</p>       <p id='5'>E</p>     </body>   </html>' HTML_END  # The selected-array is given by the application. # It consists of a sorted array with all ids of  # <p> that need to be enclosed by the <div> selected = ['2','3','4'] first_p = selected.first last_p = selected.last  # # WHAT RUBY CODE DO I NEED TO INSERT HERE TO GET # THE RESULTING HTML AS SEEN BELOW? # 

The resulting HTML should look like this (please note the inserted <div id='XYZ'>):

<html>   <body>     <p id='1'>A</p>     <div id='XYZ'>       <p id='2'>B</p>       <h1>Bla</h1>       <p id='3'>C</p>       <p id='4'>D</p>     </div>     <p id='5'>E</p>   </body> </html> 
  • 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. 2026-05-11T09:18:56+00:00Added an answer on May 11, 2026 at 9:18 am

    This is the working solution I’ve implemented into my project (Vlad@SO & Whitelist@irc#rubyonrails: Thanks for your help and inspiration.):

    require 'rubygems' require 'nokogiri'  value = Nokogiri::HTML.parse(<<-HTML_END)   '<html>     <body>       <p id='1'>A</p>       <p id='2'>B</p>       <h1>Bla</h1>       <p id='3'>C</p>       <p id='4'>D</p>       <p id='5'>E</p>     </body>   </html>' HTML_END  # The selected-array is given by the application. # It consists of a sorted array with all ids of  # <p> that need to be enclosed by the <div> selected = ['2','3','4']  # We want an elements, not nodesets! # .first returns Nokogiri::XML::Element instead of Nokogiri::XML::nodeset first_p = value.css('p##{selected.first}').first last_p = value.css('p##{selected.last}').first parent = value.css('body').first  # build and set new div_node div_node = Nokogiri::XML::Node.new('div', value) div_node['class'] = 'XYZ'  # add div_node before first_p first_p.add_previous_sibling(div_node)  selected_node = false  parent.children.each do |tag|   # if it's the first_p   selected_node = true if selected.include? tag['id']   # if it's anything between the first_p and the last_p   div_node.add_child(tag) if selected_node   # if it's the last_p   selected_node = false if selected.last == tag['id'] end  puts value.to_html 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have two tables with like below codes: Table: Accounts id | username |
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text

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.