简体   繁体   中英

XPath: Find a node within a text node

I have the following html:

<code>The first code block</code>
<p>Some text and <code>the second code block</code> followed by other text</p>

I need to find and remove all code blocks from it. I use the following XPath '//code' , but it finds only the first code block while the second remains.

Question: Why '//code' is not able to catch the second code block? How to fix it?

Details: I'm doing it in Ruby using Nokagiry . My code looks like this:

html = Nokogiri::HTML(File.read(htmlFile))
html.search('//code').remove

UPDATE:

The XPath worked in fact. I just made a mistake in different place.

Seems like You forget about iterator...
Try:

html = Nokogiri::HTML(File.read(htmlFile))
html.search('//code').each{|htm| htm.remove}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM