Friday, September 5, 2025
HomeLanguagesPHP | XMLReader getAttributeNs() Function

PHP | XMLReader getAttributeNs() Function

The XMLReader::getAttributeNs() function is an inbuilt function in PHP which is used to get the value of an attribute by name and namespace URI or an empty string if attribute does not exist or not positioned on an element node.

Syntax:

string XMLReader::getAttributeNs( string $localName, 
                               string $namespaceURI )

Parameters: This function accept two parameters as mentioned above and described below:

  • $localName: It specifies the local name.
  • $namespaceURI: It specifies the namespace URI.

Return Value: This function returns the value of attribute on success or empty string on failure.

Below examples illustrate the XMLReader::getAttributeNs() function in PHP:

Example 1:

  • data.xml




    <?xml version="1.0" encoding="utf-8"?>
    <div xmlns:x="neveropen">
        <x:h1 x:attrib="value">
          Namespaced Text 
        </x:h1>
    </div>

    
    
  • index.php




    <?php
      
    // Create a new XMLReader instance
    $XMLReader = new XMLReader();
      
    // Load the XML file
    $XMLReader->open('data.xml');
      
    // Move to next node three times
    $XMLReader->read();
    $XMLReader->read();
    $XMLReader->read();
      
    // Get the value of attribute but
    // give a wrong namespace here
    $value = $XMLReader->getAttributeNs(
        "attrib", "wrong_namespace");
      
    // Output the value to browser
    echo $value . "<br>";
      
    ?>

    
    
  • Output:
     // Empty string because namespace name doesn't match

Example 2:

  • data.xml




    <?xml version="1.0" encoding="utf-8"?>
    <div xmlns:x="my_namespace">
        <x:h1 x:attrib="value"> 
          Namespaced Text 
        </x:h1>
    </div>

    
    
  • index.php




    <?php
      
    // Create a new XMLReader instance
    $XMLReader = new XMLReader();
      
    // Load the XML file
    $XMLReader->open('data.xml');
      
    // Iterate through the XML
    while ($XMLReader->read()) {
        if ($XMLReader->nodeType == XMLREADER::ELEMENT) {
      
            // Get the value of attribute with name "attrib"
            // and namespace "my_namespace"
            $value = $XMLReader->
                   getAttributeNs("attrib", "my_namespace");
      
            // Output the value to browser
            echo $value . "<br>";
        }
    }
    ?>

    
    
  • Output:
    value

Reference: https://www.php.net/manual/en/xmlreader.getattributens.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11861 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6699 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS