Mario
Mario

Reputation: 377

php - Create element/attribute with an associated namespace

I'm looking DOMDocument::createElementNS documentation at: http://php.net/manual/en/domdocument.createelementns.php

it says that second variable "qualifiedName" has to be in defined as prefix:tagname but I found out that in some cases the prefix is added automatically (without me entering it in the code). I have made an examle:

<?php

//Namespaces url
$NS_xx = 'http://xxx';
$NS_yy = 'http://yyy';

$domxml = new DomDocument('1.0', 'UTF-8');

$Country = $domxml->appendChild ($domxml->createElementNS($NS_xx, 'xx:Country'));  // Manually entered prefix
$Country->setAttributeNS($NS_xx, 'id', '1');  // Automatically added prefix in result

$State = $Country->appendChild ($domxml->createElementNS($NS_xx,'State'));  // Automatically added prefix in result

$Region = $State->appendChild ($domxml->createElementNS($NS_yy, 'yy:Region'));  // Manually entered prefix
$Region->setAttributeNS($NS_xx, 'id', '5');  // Automatically added prefix in result

$Town = $Region->appendChild ($domxml->createElement('Town'));
$Town->appendChild ($domxml->createElementNS($NS_yy, 'F', 'New York'));  // Automatically added prefix in result
$Town->setAttributeNS($NS_xx, 'zip', '10001');  // Automatically added prefix in result

Header('Content-type: text/xml');
$domxml->formatOutput = true;
echo $domxml->saveXML();

?>  

It gives back:

<?xml version="1.0" encoding="UTF-8"?>
<xx:Country xmlns:xx="http://xxx" xx:id="1">
  <xx:State>
    <yy:Region xmlns:yy="http://yyy" xx:id="5">
      <Town xx:zip="10001">
        <yy:F>New York</yy:F>
      </Town>
    </yy:Region>
  </xx:State>
</xx:Country>

It seems to me that prefix will be added automatically if it has been previously added in any of the parent elements. Is there any reason to add that prefix anyway every time in code? If I add those prefixes manually in my code as the documentation says, the result xml will be the same...

Upvotes: 1

Views: 551

Answers (1)

dman
dman

Reputation: 1099

I found this also. DRY would suggest that if you can avoid it, you should NOT add the prefixes yourself, unless you have reason to manipulate the way the document handles namespaces.

The prefix specifically, for better code maintenance, can be defined once at the top of the doc, and at the top of your code, and won't have to be changed in too many places if you can leave it out and just use un-prefixed names.

I really don't like getting namespaces inserted ad-hoc to my docs, so whenever I can know them ahead of time, I declare them at the top before I begin, eg with

  $wrapper->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:rdfs", $rdfs_ns);

I found that if I add an element with an already-known namespace, but a different prefix (manually added) later in the doc, then items below that in the same (URI) namespace also started inheriting the more local prefix, even though it was previously using the more global one. This may be useful in some cases, though I'm not sure why.

Upvotes: 1

Related Questions