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

JRuby/CRuby incompatibility for namespaces #779

Closed
doxavore opened this issue Oct 19, 2012 · 2 comments
Closed

JRuby/CRuby incompatibility for namespaces #779

doxavore opened this issue Oct 19, 2012 · 2 comments

Comments

@doxavore
Copy link

Running this code against Nokogiri 1.5.5 (C and Java):

require 'nokogiri'
doc = Nokogiri::XML::Builder.new do |xml_base|
  xml_base.send('multistatus', 'xmlns:D' => 'DAV:') do
    xml_base.parent.namespace = xml_base.parent.namespace_definitions.first
    xml = xml_base['D']

    xml.response do
      xml.href "some url one"
    end
    xml.response do
      xml.href "some url two"
    end
  end
end

puts doc.to_xml

Results in this output for CRuby, as I would expect:

<?xml version="1.0"?>
<D:multistatus xmlns:D="DAV:">
  <D:response>
    <D:href>some url one</D:href>
  </D:response>
  <D:response>
    <D:href>some url two</D:href>
  </D:response>
</D:multistatus>

However, in JRuby, the output is:

<?xml version="1.0"?>
<D:multistatus xmlns:D="DAV:">
  <D:response>
    <href>some url one</href>
  </D:response>
  <response>
    <href>some url two</href>
  </response>
</D:multistatus>

It loses track of the namespace after a while. I'm not sure if this is the best way to do this or not, but I'm sure it shouldn't be giving different things.

@avinmathew
Copy link

I've just encountered the same issue on Nokogiri 1.6.6.2.

The namespace allocated to a child element is dependent on if a default namespace exists.

Without default namespace

Nokogiri::XML::Builder.new do |xml|
  xml.root('xmlns:p' => 'ns') do
    xml.parent.namespace = xml.parent.namespace_definitions.first
    xml.child
  end
end.doc.root.to_xml

Using MRI:

<p:root xmlns:p="ns">
  <p:child/>
</p:root>

Using JRuby:

<p:root xmlns:p="ns">
  <child/>
</p:root>

The child element is assigned a different namespace.

With default namespace

Nokogiri::XML::Builder.new do |xml|
  xml.root('xmlns:p' => 'ns', 'xmlns' => 'ns2') do
    xml.parent.namespace = xml.parent.namespace_definitions.first
    xml.child
  end
end.doc.root.to_xml

Using MRI:

<p:root xmlns:p="ns" xmlns="ns2">
  <child/>
</p:root>

Using JRuby:

<p:root xmlns="ns2" xmlns:p="ns">
  <child/>
</p:root>

The child element is assigned the same namespace.

So Nokogiri on MRI makes the assumption that if no namespace prefix is specified and there isn't a default namespace specified, then adopt the prefix of the root. Nokogiri on JRuby doesn't make that assumption.

Having a read of http://www.w3.org/TR/xml-names/#defaulting, I would lean toward the JRuby behaviour, since my understanding from the spec is that it's possible to have an element with no namespace name.

@flavorjones
Copy link
Member

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

No branches or pull requests

3 participants