简体   繁体   中英

PHP DOM: How to move element into default namespace?

What I tried and what doesn't work:

  • Input:

     $d = new DOMDocument(); $d->formatOutput = true; // Out of my control: $someEl = $d->createElementNS('http://example.com/a', 'a:some'); // Under my control: $envelopeEl = $d->createElementNS('http://example.com/default', 'envelope'); $d->appendChild($envelopeEl); $envelopeEl->appendChild($someEl); echo $d->saveXML(); $someEl->prefix = null; echo $d->saveXML(); 
  • Output is invalid XML after substitution:

     <?xml version="1.0"?> <envelope xmlns="http://example.com/default"> <a:some xmlns:a="http://example.com/a"/> </envelope> <?xml version="1.0"?> <envelope xmlns="http://example.com/default"> <:some xmlns:a="http://example.com/a" xmlns:="http://example.com/a"/> </envelope> 

Note that <a:some> may have children. One solution would be to create a new <some> , and copy all children from <a:some> to <some> . Is that the way to go?

This is really an interesting question. My first intention was to clone the <a:some> node, remove the xmlns:a attribute, remove the <a:some> and insert the clone - <a> . But this will not work, as PHP does not allow to remove the xmlns:a attribute like any regular attribute.

After some struggling with DOM methods of PHP I started to google the problem. I found this comment in the PHP documentation on this. The user suggest to write a function that clones the node manually without it's namespace:

<?php

/**
 * This function is based on a comment to the PHP documentation.
 * See: http://www.php.net/manual/de/domnode.clonenode.php#90559
 */
function cloneNode($node, $doc){
  $unprefixedName = preg_replace('/.*:/', '', $node->nodeName);
  $nd = $doc->createElement($unprefixedName);

  foreach ($node->attributes as $value)
    $nd->setAttribute($value->nodeName, $value->value);

  if (!$node->childNodes)
    return $nd;

  foreach($node->childNodes as $child) {
    if($child->nodeName == "#text")
      $nd->appendChild($doc->createTextNode($child->nodeValue));
    else
      $nd->appendChild(cloneNode($child, $doc));
  }

  return $nd;
}

Using it would lead to a code like this:

$xml = '<?xml version="1.0"?>
<envelope xmlns="http://example.com/default">
  <a:some xmlns:a="http://example.com/a"/>
</envelope>';

$doc = new DOMDocument();
$doc->loadXML($xml);

$elements = $doc->getElementsByTagNameNS('http://example.com/a', 'some');
$original = $elements->item(0);

$clone = cloneNode($original, $doc);
$doc->documentElement->replaceChild($clone, $original);

$doc->formatOutput = TRUE;
echo $doc->saveXML();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM