Hi,

I think the following behaviors of REXML can be considered as bugs but I
wanted a small confirmation before implementing some patches:

1) XML Documents accept more than 1 XMLDecl (which is not allowed by
http://www.w3.org/TR/xml/#NT-prolog)

irb(main):001:0> require 'rexml/document'
irb(main):002:0> d = REXML::Document.new("<foo />")
irb(main):004:0> d.add REXML::XMLDecl.new
irb(main):005:0> d.add REXML::XMLDecl.new
=> [<?xml ... ?>, <?xml ... ?>, <foo/>]
irb(main):006:0> d.write
<?xml version='1.0'?><?xml version='1.0'?><foo/>

This behavior can be seen with "Document#to_s" and "Document#children"
too. It goes back to:

ruby_1_9/lib/rexml/document.rb:70
  if child.kind_of? XMLDecl
        @children.unshift child
        child.parent = self
  elsif...

Since it's not verifying if a XMLDecl already exists for the document.

2) DocTypes are being added to the end of the document.

This bug has been reported in
http://www.germane-software.com/projects/rexml/ticket/120 for at least 4
months.
To add a bit more, I _think_ this condition will fail (kind_of without "?"
unless it's defined like that somewhere else) but it'll never be met:

ruby_1_9/lib/rexml/document.rb:80
  if @children[ insert_before_index ] # Not null = not end of list
    if @children[ insert_before_index ].kind_of DocType

Since insert_before_index will be the expected position + 1.

Again, as I said earlier, I'm not entirely sure if my reasonings are fine
but I know the errors are there in 1.8 and probably 1.9 (unless I'm
missing something).

Comments?

-- 
Federico