[BUG] An attribute lost a namespace.

20 views
Skip to first unread message

atori...@gmail.com

unread,
Mar 28, 2015, 1:43:11 PM3/28/15
to nokogi...@googlegroups.com
An attribute lost a namespace, when a node append an other document's element with the namespaced attribute.
I'm using Nokogiri 1.6.6.2 on CRuby 1.9.3.

Running this:

require 'nokogiri'

raw_xml = <<"EOS"
<?xml version="1.0"?>
<Root xmlns:prefix="namespace">
  <Element prefix:attribute="value"/>
  <Add/>
</Root>
EOS

src = Nokogiri.XML(raw_xml){|c|c.noblanks}
doc = Nokogiri.XML(raw_xml){|c|c.noblanks}

child = src.xpath('//Element').first
#puts child.to_xml
doc.xpath('//Add').first.add_child(child)
#puts child.to_xml
#puts doc.to_xml

__END__

I would expect doc.to_xml to produce:
<?xml version="1.0"?>
<Root xmlns:prefix="namespace">
  <Element prefix:attribute="value"/>
  <Add>
    <Element prefix:attribute="value"/>
  </Add>
</Root>

However, calling doc.to_xml actually produces:
<?xml version="1.0"?>
<Root xmlns:prefix="namespace">
  <Element prefix:attribute="value"/>
  <Add>
    <Element attribute="value"/>
  </Add>
</Root>

What's wrong with me.

Mike Dalessio

unread,
Apr 28, 2015, 7:07:22 PM4/28/15
to nokogiri-talk
Hello,

Thank you for asking this question.

I don't think anything is wrong; this mystified me for a few minutes as well. The justification for the current behavior is simply that we're not doing the comparison of namespace URIs when moving nodes between documents. I couldn't find any sort of specification stating how this should work; so we adopted this behavior, likely because it was the easiest thing to do at the time.

Worth noting that this code works fine if you're simply reparenting nodes within a document; this example displays odd behavior because the node is moving *between* documents.

If you feel strongly this behavior is wrong and should be changed, please open a Github issue requesting the behavior change.

Thanks for using Nokogiri!
 

--
You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
To post to this group, send email to nokogi...@googlegroups.com.
Visit this group at http://groups.google.com/group/nokogiri-talk.
For more options, visit https://groups.google.com/d/optout.

Mike Dalessio

unread,
Apr 28, 2015, 7:14:45 PM4/28/15
to nokogiri-talk
Ah - please note that a GH issue has been opened for this:


Cheers,
-m

Reply all
Reply to author
Forward
0 new messages