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

Namespaced attributes cause jruby failure. #869

Closed
jcoyne opened this issue Mar 19, 2013 · 3 comments
Closed

Namespaced attributes cause jruby failure. #869

jcoyne opened this issue Mar 19, 2013 · 3 comments

Comments

@jcoyne
Copy link

jcoyne commented Mar 19, 2013

Test case, using nokogiri 1.5.7

Nokogiri::XML::Builder.new do |xml|
  xml.root('xmlns:foo' => 'bar') {
    xml.obj('foo:attr' => 'baz')
  }
end.to_xml

In java:

Java::OrgW3cDom::DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
    from org.apache.xerces.dom.CoreDocumentImpl.checkDOMNSErr(Unknown Source)
    from org.apache.xerces.dom.AttrNSImpl.setName(Unknown Source)
    from org.apache.xerces.dom.AttrNSImpl.<init>(Unknown Source)
    from org.apache.xerces.dom.CoreDocumentImpl.createAttributeNS(Unknown Source)
    from org.apache.xerces.dom.ElementImpl.setAttributeNS(Unknown Source)
    from nokogiri.XmlNode.set(XmlNode.java:1213)
    from nokogiri.XmlNode$INVOKER$i$2$0$set.call(XmlNode$INVOKER$i$2$0$set.gen)

In MRI we get the expected behavior (except for the duplicate xmlns, addressed in another ticket):

"<?xml version=\"1.0\"?>\n<root xmlns:foo=\"bar\" xmlns:foo=\"bar\">\n  <obj foo:attr=\"baz\"/>\n</root>\n" 
@mbklein
Copy link
Contributor

mbklein commented Mar 20, 2013

Ah, once again this is an issue where neither Java nor C is Doing It Right™, despite MRI's appearance of success. The issue is that the <obj/> element is not yet part of the tree when its attributes are being set, so Node#set doesn't yet have access to its parent's namespace definitions. C gets around this the Old Fashioned Way, by simply creating a non-namespaced attribute node called foo:baz:

irb> Nokogiri::XML::Builder.new do |xml|
irb>   xml.root('xmlns:foo' => 'bar') {
irb>     xml.obj('foo:attr' => 'baz')
irb>   }
irb> end
irb> d.root.children.first
=> #<Nokogiri::XML::Element:0x3fe30e27f4a8 name="obj" attributes=[#<Nokogiri::XML::Attr:0x3fe30e27e9a4 name="foo:attr" value="baz">]>
irb> d.root.children.first.attribute_nodes.first.remove
=> #<Nokogiri::XML::Attr:0x3fe30e27e9a4 name="foo:attr" value="baz">
irb> d.root.children.first['foo:attr'] = 'baz'
=> "baz"
irb> d.root.children.first.attribute_nodes
=> [#<Nokogiri::XML::Attr:0x3fe30e20b2c4 name="attr" namespace=#<Nokogiri::XML::Namespace:0x3fe30e27f728 prefix="foo" href="bar"> value="baz">]

Java, on the other hand, tries to look up the foo: namespace, gets a null response, and tries to use that as a namespace URI. Boom.

I think it can be fixed by appending the newly-created element to the parent before trying to set the attribute nodes. I'll give it a shot and see if it breaks anything else.

@benlangfeld
Copy link
Contributor

This also breaks a bunch of the Blather test suite: https://travis-ci.org/adhearsion/blather/jobs/5669753

@flavorjones
Copy link
Member

Pull request #870 has been merged.

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

Successfully merging a pull request may close this issue.

4 participants