Hi everyone,
I am having trouble removing a node from a Nokogiri::XML::NodeSet, not
sure what
I should use to remove a specific node at a given position.
I have tried the following but it doesn’t seem to work:
puts nodes.length
nodes[0].remove
puts nodes.length
Ted
Greetings,
On Wed, Apr 13, 2011 at 8:06 PM, Ted F. [email protected]
wrote:
Hi everyone,
I am having trouble removing a node from a Nokogiri::XML::NodeSet, not
sure what
I should use to remove a specific node at a given position.
Nokogiri’s NodeSet implements Enumerable, and much of the Array
interface,
so you should be able to treat it much like you’d treat any Ruby Array.
For example, to remove the first node in a NodeSet, simply use
nodes.shift
. To remove the last node, use nodes.pop
. Etc.
I have tried the following but it doesn’t seem to work:
puts nodes.length
nodes[0].remove
Just a note, that the above code removes the first node in the NodeSet
from
its document. Not what you want.
Best of luck.
On Thu, Apr 14, 2011 at 3:31 PM, Ted F. [email protected]
wrote:
search for the node every time it needs to erase it.
puts nodes.length
nodes.delete(nodes[0])
puts nodes.length
You want method #remove:
irb(main):006:0> doc =
Nokogiri::XML(“12”)
=> #<Nokogiri::XML::Document:0x832a9b2 name=“document”
children=[#<Nokogiri::XML::Element:0x832a728 name=“root”
children=[#<Nokogiri::XML::Element:0x8328c20 name=“test”
children=[#<Nokogiri::XML::Text:0x8328a9a “1”>]>,
#<Nokogiri::XML::Element:0x8328914 name=“test”
children=[#<Nokogiri::XML::Text:0x832878e “2”>]>]>]>
irb(main):007:0> puts doc
<?xml version="1.0"?>
1
2
=> nil
irb(main):008:0> doc.xpath('//test')
=> [#<Nokogiri::XML::Element:0x8328c20 name="test"
children=[#]>,
#<Nokogiri::XML::Element:0x8328914 name="test"
children=[#]>]
irb(main):009:0> doc.xpath('//test[last()]')
=> [#<Nokogiri::XML::Element:0x8328914 name="test"
children=[#]>]
irb(main):010:0> doc.xpath('//test[last()]').remove
=> [#<Nokogiri::XML::Element:0x8328914 name="test"
children=[#]>]
irb(main):011:0> puts doc
<?xml version="1.0"?>
1
=> nil
Kind regards
robert
It doesn’t remove it though. I also don’t want specifically to remove
the first or last node, often I want to remove one somewhere in the
middle of the array.
puts nodes.length
nodes[0].remove
puts nodes.length
when I run that code I have originally have 4 nodes, and after, I still
have
4 nodes. I found a way to do this but I disklike it, because it has to
search for the node every time it needs to erase it.
puts nodes.length
nodes.delete(nodes[0])
puts nodes.length
Ted
Mike D. wrote in post #992653:
Greetings,
On Wed, Apr 13, 2011 at 8:06 PM, Ted F. [email protected]
wrote:
Hi everyone,
I am having trouble removing a node from a Nokogiri::XML::NodeSet, not
sure what
I should use to remove a specific node at a given position.
Nokogiri’s NodeSet implements Enumerable, and much of the Array
interface,
so you should be able to treat it much like you’d treat any Ruby Array.
For example, to remove the first node in a NodeSet, simply use
nodes.shift
. To remove the last node, use nodes.pop
. Etc.
I have tried the following but it doesn’t seem to work:
puts nodes.length
nodes[0].remove
Just a note, that the above code removes the first node in the NodeSet
from
its document. Not what you want.
Best of luck.
Ted F. wrote in post #992628:
Hi everyone,
I am having trouble removing a node from a Nokogiri::XML::NodeSet, not
sure what
I should use to remove a specific node at a given position.
I have tried the following but it doesn’t seem to work:
puts nodes.length
nodes[0].remove
puts nodes.length
Ted
Consider this: suppose I split a string into an array of substrings.
Then I delete one of the substrings from the string. Will the array
size change? For instance:
str = “0 1 2 3 4”
arr = str.split " "
p arr
–output:–
[“0”, “1”, “2”, “3”, “4”]
str.delete!(arr[2])
p str
puts arr.length
–output:–
[“0”, “1”, “2”, “3”, “4”]
“0 1 3 4”
5
Why didn’t the array size change?
I found a way to do this but I disklike it, because it
has to search for the node every time it needs to erase it.
puts nodes.length
nodes.delete(nodes[0])
puts nodes.length
Huh? nodes is a “list” (whatever that is?) of individual Node objects.
How do you expect to delete something from the list without specifying
its location?