Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[JRuby] namespaced attributes broken when appending raw xml to builder #770

Closed
flavorjones opened this Issue Sep 26, 2012 · 3 comments

Comments

Projects
None yet
3 participants
Owner

flavorjones commented Sep 26, 2012

From nokogiri-talk thread here:

      def test_raw_xml_append_with_namespaces
        doc = Nokogiri::XML::Builder.new do |xml|
          xml.root("xmlns:foo" => "x") do
            xml << '<Element foo:bar="bazz"/>'
          end
        end.doc

        el = doc.at 'Element'
        assert_not_nil el

        attr = el.attributes["bar"]
        assert_not_nil attr
        assert_not_nil attr.namespace
        assert_equal "foo", attr.namespace.prefix
      end

To give some context on this, Avin posted the following in the google group.
Running this:

raw_xml = '<Element prefix:attribute="value"/>'
builder = Nokogiri::XML::Builder.new do |xml|
  xml.Root("xmlns:prefix" => "namespace") do
    xml << raw_xml
  end
end

I would expect builder.doc.to_xml to produce:

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

However, calling builder.doc.to_xml actually produces:

<?xml version="1.0"?>
<Root xmlns:prefix="namespace">
  <element attribute="value"/>
</Root>

Note the lower case element and missing attribute prefix.

Interestingly, if I change the case to:

raw_xml = '<Element prefix:attribute="value"/>'
builder = Nokogiri::XML::Builder.new do |xml|
  xml.Root("xmlns:prefix" => "namespace")
end
builder.doc.root << raw_xml

Then builder.doc.to_xml will have the correctly cased element, but is still missing the attribute prefix.

Owner

flavorjones commented Oct 1, 2012

I think my test case captures the requirements. If it doesn't, then please submit a revised test case that does.

Member

jvshahid commented Oct 21, 2012

There are two problems that lead to the failure. First, the current Java implementation of XmlNode::in_context() doesn't parse the xml in the context of the root node, therefore the prefix 'foo' is left unbound. The second problem is in XmlNode::parse. There's a hack that deals with issue #313 by falling back to parsing the XML as HTML. This causes the tag to loose its case and ignores the unknown prefix 'foo' (remember in_context is still not doing the right thing but the HTML parser is more permissive).

Ideally I'd have used Xerces to parse the given XML in context of another node. Unfortunately, this feature isn't implemented yet https://issues.apache.org/jira/browse/XERCESJ-1429. I'm still trying to work around the problem. @yokolet, any input would be appreciated.

@jvshahid jvshahid added a commit that referenced this issue Oct 21, 2012

@jvshahid jvshahid Fix issue #770.
DocumentFragment parsing which depends on Xml::Node::parse didn't
properly handle namespaces in context. Ideally we should be using
Xerces parsing with a hint to the context node, but unfortunately
this feature isn't implemented yet
https://issues.apache.org/jira/browse/XERCESJ-1429 which is why we
manually add namespace declarations to the fake root that encapsulates
the document fragment.
0b07e6e

@jvshahid jvshahid closed this Oct 21, 2012

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