-
-
Notifications
You must be signed in to change notification settings - Fork 897
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
NodeSet does not follow ruby conventions for enumerable methods #1677
Comments
Change your assignment line to: dup_nodes = Nokogiri::XML::NodeSet.new(Nokogiri::XML::Document.new, duplicates.to_xml) And I suspect you get the desired behavior. Also, you can cut out some middle steps by applying uidsA = docA.xpath('//REC/UID').map(&:text) and Array already has an intersection operator in Ruby, so you don't need to convert to uid_intersection = uidsA & uidsB
=> ["WOS:A2"] |
Hi, thanks for telling us about your experiences using Nokogiri. You're performing some interesting and unexpected operations, and so I'm curious if Nokogiri's behavior is really getting in your way, or if you're just being curious/pedantic. To help me understand, could you tell me more about what you're trying to do with Nokogiri? By this question, I mean that it's more interesting to me to get users from point A to point B efficiently than to figure out how to prevent users from breaking some expectations that Nokogiri has that may not be obvious. |
To step back a bit, I wanted to merge one set of I was trying to work with a Nokogiri document as an immutable instance. I was using There are some details at |
I'm sorry, I'm not totally following. I looked at the code, and the specs, and I understand what you're trying to do ... do these tests pass? If so, can you help me understand what Nokogiri is doing that's unexpected by writing a failing test? |
Using
~/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/nokogiri-1.8.1
In general, the ruby enumerable methods, like
Array.select
, return a new Array, leaving the original untouched. For example,Similarly, another enumerable operation does not touch the original:
It's reasonable to expect the same behavior of enumerable methods on the NodeSet, right? Let's see.
Create two sets of XML, with a duplicate
REC
element in each (WOS:A2
):Now lets try to gather the duplicate records into a new NodeSet.
The
dup_nodes.count => 1
and the first line above shows that it has the UID=‘WOS:A2’. So, how can anxpath('//UID')
return two different UID values from this NodeSet that seems to only contain one UID?The only way that
dup_nodes.xpath('//UID')
could return two results withWOS:A2
andWOS:B2
is if thedup_nodes
is somehow still connected to the parent doc that it was derived from, despite aNodeSet.select
operation that should not touch the original node set and should return a new object. Although I haven't yet proved it in a similar small example, I believe similar problems beset theNodeSet.dup
method.The text was updated successfully, but these errors were encountered: