@@ -421,6 +421,7 @@ public class XmlNode extends RubyObject
421
421
}
422
422
423
423
Element e = (Element ) node ;
424
+ Node parent = e .getParentNode ();
424
425
425
426
// disable error checking to prevent lines like the following
426
427
// from throwing a `NAMESPACE_ERR' exception:
@@ -439,22 +440,27 @@ public class XmlNode extends RubyObject
439
440
set_namespace (context , ((XmlNode )parent (context )).namespace (context ));
440
441
}
441
442
} else {
442
- String currentPrefix = e .getParentNode ().lookupPrefix (nsURI );
443
- String currentURI = e .getParentNode ().lookupNamespaceURI (prefix );
444
- boolean isDefault = e .getParentNode ().isDefaultNamespace (nsURI );
445
-
446
443
// add xmlns attribute if this is a new root node or if the node's
447
444
// namespace isn't a default namespace in the new document
448
- if (e . getParentNode () .getNodeType () == Node .DOCUMENT_NODE ) {
445
+ if (parent == null || parent .getNodeType () == Node .DOCUMENT_NODE ) {
449
446
// this is the root node, so we must set the namespaces attributes anyway
450
447
e .setAttribute (prefix == null ? "xmlns" : "xmlns:" + prefix , nsURI );
451
448
} else if (prefix == null ) {
452
449
// this is a default namespace but isn't the default where this node is being added
453
- if (!isDefault ) { e .setAttribute ("xmlns" , nsURI ); }
454
- } else if (!prefix .equals (currentPrefix ) || !nsURI .equals (currentURI )) {
455
- // this is a prefixed namespace
456
- // but doesn't have the same prefix or the prefix is set to a different URI
457
- e .setAttribute ("xmlns:" + prefix , nsURI );
450
+ boolean isDefault = parent .isDefaultNamespace (nsURI );
451
+
452
+ if (!isDefault ) {
453
+ e .setAttribute ("xmlns" , nsURI );
454
+ }
455
+ } else if (parent .getNodeType () == Node .ELEMENT_NODE ) {
456
+ String currentPrefix = parent .lookupPrefix (nsURI );
457
+ String currentURI = parent .lookupNamespaceURI (prefix );
458
+
459
+ if (!prefix .equals (currentPrefix ) || !nsURI .equals (currentURI )) {
460
+ // this is a prefixed namespace
461
+ // but doesn't have the same prefix or the prefix is set to a different URI
462
+ e .setAttribute ("xmlns:" + prefix , nsURI );
463
+ }
458
464
}
459
465
}
460
466
@@ -492,7 +498,7 @@ public class XmlNode extends RubyObject
492
498
493
499
// if this namespace is a duplicate of what's already in the document, remove it.
494
500
// a "duplicate" here is if the prefix and the URI both match what resolves in the parent.
495
- if (e . getParentNode () .getNodeType () == Node .ELEMENT_NODE ) {
501
+ if (parent != null && parent .getNodeType () == Node .ELEMENT_NODE ) {
496
502
RubyArray <?> nsdefs = this .namespace_definitions (context );
497
503
for (int j = 0 ; j < nsdefs .getLength () ; j ++) {
498
504
XmlNamespace ns = (XmlNamespace )nsdefs .get (j );
@@ -1524,6 +1530,8 @@ public class XmlNode extends RubyObject
1524
1530
// and keep the return value. -mbklein
1525
1531
String new_name = NokogiriHelpers .newQName (ns .getPrefix (), node );
1526
1532
this .node = NokogiriHelpers .renameNode (node , ns .getHref (), new_name );
1533
+
1534
+ this .relink_namespace (context );
1527
1535
}
1528
1536
1529
1537
clearXpathContext (getNode ());
0 commit comments