【发布时间】:2017-06-19 01:30:06
【问题描述】:
XMLWriter 出现故障,因为它正在生成命名空间声明 导致实例格式不正确。
例如:
$writer = new XMLWriter();
$writer->openUri( 'php://output' );
$writer->startIndent( true );
$writer->setIndentString ( "\t" );
$writer->startDocument( '1.0', 'UTF-8' );
$writer->startElement( 'root' );
$writer->writeAttributeNs( 'xmlns', 'bar', 'http://www.w3.org/2000/xmlns/', 'http://example.com' );
$writer->startElementNs( 'bar', 'baz', null );
$writer->endElement();
生产
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:bar="http://example.com" xmlns:xmlns="http://www.w3.org/2000/xmlns/">
<bar:baz/>
</root>
注意xmlns:xmlns="http://www.w3.org/2000/xmlns/",其中:
- 在我写的 PHP 代码中不存在
- 与Namespace constraint: Reserved Prefixes and Namespace Names 发生冲突
无论我做什么,如果我创建 any 命名空间声明,那么 XMLWriter 会输出这个非法的命名空间声明(除了我希望它输出的命名空间声明之外)。
我错过了什么?因为按原样,这会使 XMLWriter 无法使用。
编辑:以防万一,我使用的是 PHP 5.4.16 和 libxml2 2.7.8。
【问题讨论】: