當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP XMLWriter startAttributeNs()用法及代碼示例

XMLWriter::startAttributeNs()函數是PHP中的一個內置函數,用於啟動命名空間屬性。稍後可以使用XMLWriter::endAttribute()函數關閉此屬性。通常,網頁樣式無法在命名空間屬性中使用。

用法:

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

參數:此函數接受上述和以下所述的三個參數:



  • $prefix:它指定名稱空間的前綴。
  • $name:它指定名稱空間的名稱。
  • $uri:它指定名稱空間的值。

返回值:如果成功,此函數將返回TRUE,否則將返回FALSE。

以下示例說明了PHP中的XMLWriter::startAttributeNs()函數:

範例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(); 
?>

輸出:

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

範例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 GeeksforGeeks'); 
   
// 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 GeeksforGeeks'); 
   
// End the element 
$writer->endElement(); 
   
// End the document 
$writer->endDocument(); 
?>

輸出:

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




相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | XMLWriter startAttributeNs() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。