Saturday, November 22, 2025
HomeLanguagesPHP | XMLWriter startAttributeNs() Function

PHP | XMLWriter startAttributeNs() Function

The XMLWriter::startAttributeNs() function is an inbuilt function in PHP which is used to start namespaced attribute. This attribute can be later closed with XMLWriter::endAttribute() function. Usually styling web pages doesn’t works in a namespace attribute.

Syntax:

bool XMLWriter::startAttributeNs( string $prefix, 
string $name, string $uri )

Parameters: This function accepts three parameters as mentioned above and described below:

  • $prefix: It specifies the prefix for the namespace.
  • $name: It specifies the name for the namespace.
  • $uri: It specifies the value for the namespace.

Return Value: This function returns TRUE on success or FALSE on failure.

Below examples illustrate the XMLWriter::startAttributeNs() function in PHP:

Example 1:




<?php
  
// Create a new XMLWriter instance
$writer = new XMLWriter();
   
// Create the output stream as PHP
$writer->openURI('php://output');
   
// Start the document
$writer->startDocument('1.0', 'UTF-8');
   
// Start a element
$writer->startElement('div');
   
// Start the namespaced attribute
$writer->startAttributeNs('pre', 'attrib', 'value');
   
// Add value to the attribute
$writer->text('value');
   
// End the attribute
$writer->endAttribute();
   
// End the element
$writer->endElement();
   
// End the document
$writer->endDocument();
?>


Output:

<?xml version="1.0" encoding="UTF-8"?>
<div pre:attrib="value" xmlns:pre="value"/>

Example 2:




<?php
  
// Create a new XMLWriter instance
$writer = new XMLWriter();
   
// Create the output stream as PHP
$writer->openURI('php://output');
   
// Start the document
$writer->startDocument('1.0', 'UTF-8');
   
// Start a element
$writer->startElement('div');
   
// Start the namespaced attribute with style attribute
// This will not work because it is namespaced
$writer->startAttributeNs('style', 'attrib', 'value');
   
// Add value to the attribute
$writer->text('color:blue');
   
// End the attribute
$writer->endAttribute();
  
// Add value to the element
$writer->text('Namespaced neveropen');
   
// End the element
$writer->endElement();
  
// Start a h1 element
$writer->startElement('h1');
   
// Start the style attribute
$writer->startAttribute('style');
   
// Add value to the attribute
$writer->text('color:green');
   
// End the attribute
$writer->endAttribute();
   
// Add value to the element
$writer->text('Normal neveropen');
   
// End the element
$writer->endElement();
   
// End the document
$writer->endDocument();
?>


Output:

Reference: https://www.php.net/manual/en/function.xmlwriter-start-attribute-ns.php

RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6848 POSTS0 COMMENTS