Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nokogiri::XML Node content returns content of child elements as well #1435

Closed
msmock opened this issue Feb 27, 2016 · 1 comment
Closed

Nokogiri::XML Node content returns content of child elements as well #1435

msmock opened this issue Feb 27, 2016 · 1 comment

Comments

@msmock
Copy link

msmock commented Feb 27, 2016

If I try to read the text content of a XML node, I get the the text content of all child elements as well.
Check for example:

<text> A additional note: <content ID="ap1">This is a generated Text, just for demo purposes (section 2: Active Problems) </content> </text>

where the .content or .text message on the text node returns:

A additional note:
This is a generated Text,
just for demo purposes (section 2: Active Problems)

with the child tags removed.

But I expected to get one child element on the node and the text content to be 'A additional note:' only.

@flavorjones
Copy link
Member

Hi @msmock,

Thanks for asking this question. The short answer is that Nokogiri is functioning as designed, as #content should provide all the text content of all child nodes (recursively).

If you want the text from just the first child node, then you should select just the first child node. Here's one example of how to select the first child that's a text node:

#! /usr/bin/env ruby

require 'nokogiri'

xml = %Q{<text> A additional note: <content ID="ap1">This is a generated Text, just for demo purposes (section 2: Active Problems) </content> </text>}

doc = Nokogiri::XML::Document.parse xml

puts doc.root.children.find {|n| n.text?}

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants