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

remove_namespace! doesn't work in pure java version #492

Closed
Zindar opened this issue Jul 12, 2011 · 5 comments
Closed

remove_namespace! doesn't work in pure java version #492

Zindar opened this issue Jul 12, 2011 · 5 comments

Comments

@Zindar
Copy link

Zindar commented Jul 12, 2011

The following code gives an error in pure java version but works fine in MRI.

require 'rubygems'
require 'nokogiri'

xml = <<EOF
<?xml version="1.0"?>
<MetadataDocument xmlns="http://bagfors.nu/schema/test">
    <field>
      <name>Title</name>
      <value>Generic</value>
    </field>
</MetadataDocument>
EOF
doc = Nokogiri::XML xml
puts doc
doc.remove_namespaces!
In jruby it gives:
    null:-1:in `setName': org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
        from null:-1:in `rename'
        from null:-1:in `renameNode'
        from XmlDocument.java:354:in `removeNamespceRecursively'
        from XmlDocument.java:360:in `removeNamespceRecursively'
        from XmlDocument.java:340:in `remove_namespaces'
        from XmlDocument$i$0$0$remove_namespaces.gen:65535:in `call'
        from CachingCallSite.java:292:in `cacheAndCall'
        from CachingCallSite.java:135:in `call'
        from /Users/erik/nokogiri-test.rb:15:in `__file__'
        from /Users/erik/nokogiri-test.rb:-1:in `load'
        from Ruby.java:671:in `runScript'
        from Ruby.java:575:in `runNormally'
        from Ruby.java:424:in `runFromMain'
        from Main.java:278:in `doRunFromMain'
        from Main.java:198:in `internalRun'
        from Main.java:164:in `run'
        from Main.java:148:in `run'
        from Main.java:128:in `main'

This is jruby 1.6.3 and nokogiri 1.5.0 java.

@yokolet
Copy link
Member

yokolet commented Jul 28, 2011

Fixed in rev. e761784

In Java, namespace declaration is an attribute node. So, when attribute's name is "xmlns," that node should be removed in this case.

Thanks for reporting and giving a good sample.

@yokolet yokolet closed this as completed Jul 28, 2011
@brodock
Copy link

brodock commented Oct 21, 2011

could you please generate a new java's gem version, as the lastest one (1.5.0) doesn't include this essential fix?

@steeve
Copy link

steeve commented Nov 1, 2011

+1 ! thanks!

@flavorjones
Copy link
Member

There are currently still other open issues blocking a 1.5.1 release, particularly for JRuby. We're trying to get things cleaned up as quickly as we can. Thanks for your patience.

@joegoggins
Copy link

Though it will indeed be sweet to have the remove_namespaces! method working in the 1.5.1 release, you can easily get around this problem by not using the remove_namespaces! method
See documentation on "Namespaces" here: http://nokogiri.org/tutorials/searching_a_xml_html_document.html

in 10 minutes I was able to circumvent my problem, my code essentially went from:

doc = Nokogiri::XML.parse(self.response.body)
doc.remove_namespaces!
doc.xpath('//DARWIN_JOBS').children........

TO

doc = Nokogiri::XML.parse(response.body)
relevant_elements = doc.xpath('//poo:DARWIN_JOBS','poo' => 'http://SOME_URL_YOU_TAKE_OUT_OF_THE_xmlns_attribute_OF_A_PARENT_ELEMENT')
relevant_elements.children......

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

No branches or pull requests

6 participants