lib/traject/nokogiri_reader.rb
# In MRI Nokogiri, this is as simple as `new_parent_doc.root = node`
# It seemed maybe safer to dup the node as well as remove the original from the original doc,
# but I believe this will result in double memory usage, as unlinked nodes aren't GC'd until
# their doc is.  I am hoping this pattern results in less memory usage.
# https://github.com/sparklemotion/nokogiri/issues/1703
#
# However, in JRuby it's a different story, JRuby doesn't properly preserve namespaces
# when re-parenting a node.
# https://github.com/sparklemotion/nokogiri/issues/1774
#
# The nodes within the tree re-parented _know_ they are in the correct namespaces,
# and xpath queries require that namespace, but the appropriate xmlns attributes
# aren't included in the serialized XML. This JRuby-specific code seems to get
# things back to a consistent state.
In MRI Nokogiri, this is as simple as `new_parent_doc.root = node`
It seemed maybe safer to dup the node as well as remove the original from the original doc,
but I believe this will result in double memory usage, as unlinked nodes aren't GC'd until
their doc is.  I am hoping this pattern results in less memory usage.

However, in JRuby it's a different story, JRuby doesn't properly preserve namespaces
when re-parenting a node.

The nodes within the tree re-parented _know_ they are in the correct namespaces,
and xpath queries require that namespace, but the appropriate xmlns attributes
aren't included in the serialized XML. This JRuby-specific code seems to get
things back to a consistent state.
In MRI Nokogiri this is as simple as ` new_parent_doc . root = node ` It seemed maybe safer to dup the node as well as remove the original from the original doc but I believe this will result in double memory usage as unlinked nodes aren ' t GC ' d until their doc is . I am hoping this pattern results in less memory usage . However in JRuby it ' s a different story JRuby doesn ' t properly preserve namespaces when re - parenting a node . The nodes within the tree re - parented _know_ they are in the correct namespaces and xpath queries require that namespace but the appropriate xmlns attributes aren ' t included in the serialized XML . This JRuby - specific code seems to get things back to a consistent state .
def reparent_node_to_root(new_parent_doc, node)
      if Traject::Util.is_jruby?
        original_ns_scopes = node.namespace_scopes
      end
      new_parent_doc.root = node
      if Traject::Util.is_jruby?
        original_ns_scopes.each do |ns|
          if new_parent_doc.at_xpath("//#{ns.prefix}:*", ns.prefix => ns.href)
            new_parent_doc.root.add_namespace(ns.prefix, ns.href)
          end
        end
      end
      return new_parent_doc
    end
def reparent_node_to_root ( new_parent_doc , node ) if Traject :: Util . is_jruby? original_ns_scopes = node . namespace_scopes end new_parent_doc . root = node if Traject :: Util . is_jruby? original_ns_scopes . each do | ns | if new_parent_doc . at_xpath ( "//#{ns.prefix}:*" , ns . prefix => ns . href ) new_parent_doc . root . add_namespace ( ns . prefix , ns . href ) end end end return new_parent_doc end
In MRI Nokogiri, this is as simple as `new_parent_doc.root = node`
It seemed maybe safer to dup the node as well as remove the original from the original doc,
but I believe this will result in double memory usage, as unlinked nodes aren't GC'd until
their doc is.
In MRI Nokogiri this is as simple as ` new_parent_doc . root = node ` It seemed maybe safer to dup the node as well as remove the original from the original doc but I believe this will result in double memory usage as unlinked nodes aren ' t GC ' d until their doc is .
new_parent_doc
<not_specific>
node
<not_specific>