A função DOMDocument::createAttributeNS() é uma função embutida no PHP que é usada para criar um novo nó de atributo com um namespace associado.

Sintaxe:

DOMAttr DOMDocument::createAttributeNS( string $namespaceURI, string $qualifiedName )

Parâmetros: esta função aceita dois parâmetros conforme mencionado acima e descrito abaixo:

  • $namespaceURI: este parâmetro contém o URI do namespace.
  • $QualityName Este parâmetro contém o nome da tag e o prefixo do atributo. Por exemplo: prefix: tagname.

Valor de retorno: esta função retorna um novo objeto DOMAttr em caso de sucesso ou FALSE em caso de falha.

O programa abaixo ilustra a função DOMDocument::createAttributeNS() em PHP:

Programa:

<?php
  
// Store the XML document to the source
$source = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<root><contact><email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile></contact></root>
XML;
  
// Create a new document
$domDocument = new DOMDocument( '1.0' );
  
// Load the XML file
$domDocument->loadXML( $source );
      
// Use createAttributeNS() function to create new attribute
// node with an associated namespace
$attrNS = $domDocument->createAttributeNS(
                '{namespace}', 'info:cont_info' );
  
// Create XML document and display it
echo $domDocument->saveXML() . "\n";
  
// Assign value to the createAttributeNS
$attrNS->value = 'https://www.geeksforgeeks.org/about/contact-us/';
  
$domDocument->getElementsByTagName( 'contact' )
            ->item(0)->appendChild( $attrNS );
      
// Create XML document and display it
print $domDocument->saveXML() . "\n";
      
?>
Saída:
<? xml version = "1.0" encoding = "UTF-8"?>
<root xmlns: info = "{namespace}">
    <contato>
        <email> abc@geeksforgeeks.org </email>
        <mobile> + 91-987654321 </mobile>
    </contact>
</root>

<? xml version = "1.0" encoding = "UTF-8"?>
<root xmlns: info = "{namespace}">
    <informações de contato: cont_info =
        "https://www.geeksforgeeks.org/about/contact-us/">
        <email> abc@geeksforgeeks.org </email>
        <mobile> + 91-987654321 </mobile>
    </contact>
</root>

Referência: https://www.php.net/manual/en/domdocument.createattributens.php