DOMDocument::createAttributeNS

(PHP 5, PHP 7, PHP 8)

DOMDocument::createAttributeNS โ€” ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸๅๅ‰็ฉบ้–“ใซๆ–ฐใ—ใ„ๅฑžๆ€งใ‚’ไฝœๆˆใ™ใ‚‹

่ชฌๆ˜Ž

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

ใ“ใฎ้–ขๆ•ฐใฏใ€DOMAttr ใ‚ฏใƒฉใ‚นใฎๆ–ฐใ—ใ„ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ ใ“ใฎใƒŽใƒผใƒ‰ใฏใ€( DOMNode::appendChild() ใชใฉใง) ๆŒฟๅ…ฅใ•ใ‚Œใชใ„้™ใ‚Šใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆๅ†…ใซใ‚ใ‚‰ใ‚ใ‚Œใพใ›ใ‚“ใ€‚

ใƒ‘ใƒฉใƒกใƒผใ‚ฟ

namespace

ๅๅ‰็ฉบ้–“ใฎ URIใ€‚

qualifiedName

ๅฑžๆ€งใฎใ‚ฟใ‚ฐๅใจใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใ‚’ใ€prefix:tagname ใฎใ‚ˆใ†ใชๅฝขๅผใงๆŒ‡ๅฎšใ™ใ‚‹ใ€‚

ๆˆปใ‚Šๅ€ค

ๆ–ฐใ—ใ„ DOMAttrใ€ใ‚ใ‚‹ใ„ใฏใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใŸๅ ดๅˆใฏ false ใ‚’่ฟ”ใ—ใพใ™ใ€‚

ใ‚จใƒฉใƒผ / ไพ‹ๅค–

ไปฅไธ‹ใฎใ‚จใƒฉใƒผใ‚ณใƒผใƒ‰ใง DOMException ใ‚’ใ‚นใƒญใƒผใ—ใพใ™:

DOM_INVALID_CHARACTER_ERR

qualifiedName ใŒ็„กๅŠนใชๆ–‡ๅญ—ใ‚’ๅซใ‚“ใงใ„ใ‚‹ๅ ดๅˆใซ็™บ็”Ÿใ—ใพใ™ใ€‚

DOM_NAMESPACE_ERR

qualifiedName ใŒไธๆญฃใชๅฝขๅผใงใ‚ใ‚‹ๅ ดๅˆใ€ใ‚ใ‚‹ใ„ใฏ qualifiedName ใŒใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใ‚’ๅซใ‚“ใงใ„ใ‚‹ ใซใ‚‚ใ‹ใ‹ใ‚ใ‚‰ใš namespace ใŒ null ใงใ‚ใ‚‹ๅ ดๅˆใซ็™บ็”Ÿใ—ใพใ™ใ€‚

ๅค‰ๆ›ดๅฑฅๆญด

ใƒใƒผใ‚ธใƒงใƒณ ่ชฌๆ˜Ž
8.3.0 prefix ใ‚’ๆŒ‡ๅฎšใ›ใšใซใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ‚ณใƒผใƒซใ—ใฆใ‚‚ใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅๅ‰็ฉบ้–“ใ‚’ไปฎๅฎšใ™ใ‚‹ไปฃใ‚ใ‚Šใซ prefix ใ‚’้ธใถใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ใ“ใ‚Œใ‚ˆใ‚Šๅ‰ใฎใƒใƒผใ‚ธใƒงใƒณใงใฏใ€prefix ใชใ—ใฎๅฑžๆ€งใ‚’ไฝœๆˆใ—ใ€่ชคใฃใฆใ‚ชใƒผใƒŠใƒผใจใชใ‚‹่ฆ็ด ใฎๅๅ‰็ฉบ้–“ใ‚’้ฉ็”จใ—ใฆใ„ใพใ—ใŸใ€‚ใชใœใชใ‚‰ใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅๅ‰็ฉบ้–“ใฏๅฑžๆ€งใซใฏ้ฉ็”จใ•ใ‚Œใชใ„ใŸใ‚ใงใ™ใ€‚
8.3.0 ใ‚ชใƒผใƒŠใƒผใจใชใ‚‹่ฆ็ด ใง prefix ใŒๆ—ขใซ็•ฐใชใ‚‹ namespace URI ใงไฝฟใ‚ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใซใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใ‚‚ใ€ๅๅ‰็ฉบ้–“ใฎ่ก็ชใ‚’้ฟใ‘ใ‚‹ใŸใ‚ใซๆ–ฐใ—ใ prefix ใ‚’็•ฐใชใ‚‹ใ‚‚ใฎใซๅค‰ๆ›ดใ™ใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ใ“ใ‚Œใฏ DOM ใฎไป•ๆง˜ใŒ่ฆๆฑ‚ใ™ใ‚‹ๆŒฏใ‚‹่ˆžใ„ใซๅˆใ‚ใ›ใ‚‹ใŸใ‚ใงใ™ใ€‚ใ“ใ‚Œใ‚ˆใ‚Šๅ‰ใฎใƒใƒผใ‚ธใƒงใƒณใงใฏใ€ใ‚จใƒฉใƒผใ‚ณใƒผใƒ‰ DOM_NAMESPACE_ERR ใ‚’ๆŒใค DOMException ใ‚’ใ‚นใƒญใƒผใ—ใฆใ„ใพใ—ใŸใ€‚

ๅ‚่€ƒ

๏ผ‹add a note

User Contributed Notes 1 note

up
10
_ michael ยถ
16 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>
      
    */
    
?>