DOMDocument::createAttributeNS

(PHP 5, PHP 7, PHP 8)

DOMDocument::createAttributeNS Crea un nuevo atributo con un espacio de nombres asociado

Descripción

public DOMDocument::createAttributeNS(?string $namespace, string $qualifiedName): DOMAttr|false

Esta función crea una nueva instancia de la clase DOMAttr. Este nodo no se mostrará en el documento a no ser que sea insertado con (p.e.j.) DOMNode::appendChild().

Parámetros

namespace

El URI del espacio de nombres.

qualifiedName

El nombre de la etiqueta y el prefijo del atributo, en este formato: prefijo:nombreEtiqueta.

Valores devueltos

El nuevo DOMAttr o false si ocurre un error.

Errores/Excepciones

DOM_INVALID_CHARACTER_ERR

Lanzado si qualifiedName contiene un carácter inválido.

DOM_NAMESPACE_ERR

Lanzado si qualifiedName es un nombre cualificado mal formado o si qualifiedName tiene un sufijo y namespace es null.

Historial de cambios

Versión Descripción
8.3.0 Llamar a este método sin especificar un prefijo elegirá ahora un prefijo en lugar de asumir el espacio de nombres por defecto. Anteriormente, esto creaba un atributo sin prefijo y aplicaba incorrectamente el espacio de nombres al elemento propietario ya que los espacios de nombres por defecto no se aplican a los atributos.
8.3.0 Llamar a este método utilizando un prefijo ya declarado en el elemento propietario con un URI de espacio de nombres diferente cambiará ahora el nuevo prefijo para evitar conflictos de espacio de nombres. Esto alinea el comportamiento con la especificación del DOM. Anteriormente, esto lanzaba una DOMException con el código DOM_NAMESPACE_ERR.

Ver también

add a note

User Contributed Notes 1 note

up
10
_ michael
15 years ago
If a new namespace is introduced while creating and inserting an attribute, createAttributeNS() does not behave in the same way as createElementNS().

(1) Location: With createAttributeNS(), the new namespace is declared at the level of the document element. By contrast, createElementNS() declares the new namespace at the level of the affected element itself.

(2) Timing: With createAttributeNS(), the new namespace is declared in the document as soon as the attribute is created - the attribute does not actually have to be inserted. createElementNS() doesn't affect the document as long as the element is not inserted.

An example:

<?php

$source
= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<root><tag></tag></root>
XML;

/*

I. createAttributeNS:
* a new namespace shows up immediately, even without insertion of the attribute
* the new namespace is declared at the level of the document element

*/

$doc = new DOMDocument( '1.0' );
$doc->loadXML( $source );

// (1) We just create a "namespace'd" attribute without appending it to any element.
$attr_ns = $doc->createAttributeNS( '{namespace_uri_here}', 'example:attr' );

print
$doc->saveXML() . "\n";

/*
Result: The namespace declaration appears, having been added to the document element. Output:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:example="{namespace_uri_here}"><tag/></root>

*/

// (2) Next, we give the attribute a value and insert it.
$attr_ns->value = 'value';
$doc->getElementsByTagName( 'tag' )->item(0)->appendChild( $attr_ns );

print
$doc->saveXML() . "\n";

/*
Result: The "namespace'd" attribute shows up as well. Output:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:example="{namespace_uri_here}"><tag example:attr="value"/></root>

*/

/*

II. createElementNS:
* a new namespace shows up only when the element is inserted
* the new namespace is declared at the level of the inserted element

*/

$doc = new DOMDocument( '1.0' );
$doc->loadXML( $source );

// (1) We create a "namespace'd" element without inserting it into the document.
$elem_ns = $doc->createElementNS( '{namespace_uri_here}', 'example:newtag' );

print
$doc->saveXML() . "\n";

/*
Result: The document remains unchanged. Output:

<?xml version="1.0" encoding="UTF-8"?>
<root><tag/></root>

*/

// (2) Next, we insert the new element.
$doc->getElementsByTagName( 'tag' )->item(0)->appendChild( $elem_ns );

print
$doc->saveXML() . "\n";

/*
Result: The namespace declaration appears, and it is embedded in the element using it. Output:

<?xml version="1.0" encoding="UTF-8"?>
<root><tag><example:newtag xmlns:example="{namespace_uri_here}"/></tag></root>

*/

?>
To Top