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

  • Home
  • SEARCH
  • 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 5950647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:25:24+00:00 2026-05-22T17:25:24+00:00

I have the following HTML: <ul class=filtering_new width=50%> <li class=filter>1</li> <li class=filter>2</li> <script>Alert(‘1’);</script> <li

  • 0

I have the following HTML:

<ul class="filtering_new" width="50%">
     <li class="filter">1</li>
     <li class="filter">2</li>
     <script>Alert('1');</script>
     <li class="filter">3</li>
</ul>

How can I get li with inner_html = 3?

I tried like this:

page.search("//ul.filtering_new").each do |list|
     puts list.search("li").size  
end

where page is the HTML document.

size = 2, but it should be 3.

I tried to do like in manual https://github.com/hpricot/hpricot/wiki/hpricot-challenge
but I cannot even find <script.

 list.search("script")

returns nothing.

  • 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-22T17:25:26+00:00Added an answer on May 22, 2026 at 5:25 pm

    Most XML/HTML parsing in Ruby uses Nokogiri these days, so I’ll recommend that parser. However, both Hpricot and Nokogiri support XPath and CSS, so they are fairly interchangeable.

    I’d go about it this way:

    html = <<EOT
    <ul class="filtering_new" width="50%">
         <li class="filter">1</li>
         <li class="filter">2</li>
         <script>Alert('1');</script>
         <li class="filter">3</li>
    </ul>
    EOT
    
    require 'nokogiri'
    
    doc = Nokogiri::HTML(html)
    li = doc.search('//li[@class="filter"]').select{ |n| n.text.to_i == 3 } 
    li # => [#<Nokogiri::XML::Element:0x8053fc84 name="li" attributes=[#<Nokogiri::XML::Attr:0x8053fb6c name="class" value="filter">] children=[#<Nokogiri::XML::Text:0x80546f98 "3">]>]
    

    That finds the candidate nodes, then returns them as a NodeSet to be iterated over, where they are selected/rejected based on the node’s text.

    li = doc.search('//li[text() = "3"]') 
    li # => [#<Nokogiri::XML::Element:0x8053fc84 name="li" attributes=[#<Nokogiri::XML::Attr:0x8053fb6c name="class" value="filter">] children=[#<Nokogiri::XML::Text:0x80546f98 "3">]>]
    

    That offloads more of the comparison to the underlying libXML library, where it runs a lot faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following html page: <body> <div class=hide1 style=width:1000px; height:1000px;> <table width=100% border=0
I have the following HTML... <body onload=draw();> <p><a href=><canvas class=demo2 width=6 height=12>Fallback</canvas> Back to
I have the following html and i am using php's DomDocument class to get
I have the following html <div id=menu> <ul class=horizMenu> <li id=active><a href=# id=current>About</a></li> <li><a
I have the following HTML: <tr> <td class=ui-widget-content> <a title=Edit href=/Administration/Menus/Edit/001W class=editLink ui-button ui-widget
I have the following html that I am trying to modify: <div class=col1 width8><img
I have the following HTML. <div class=red>Div One</div> <div class=green>Div Two</div> <div class=blue>Div Three</div>
I have the following HTML: <div class=wrapper> <div class=label> foor bar baz </div> <img
I have the following HTML (a snippet): <td class=ms-vb-title height=100%> <table class=ms-unselectedtitle onmouseover=OnItem(this) ctxname=ctx1
Let's say we have the following html: <ul id=portfolio-list class=clearfix span-24> <li class=2012#foo bar

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.