我想使用 XMLWriter 在 XML 文档之上生成嵌套实体 DTD 声明。我从没有 XMLWriter 的字符串构建代码开始,它也说明了所需的输出:
<?php
$sXML = "<!DOCTYPE Example PUBLIC \"urn:example:example.org:20110823:Example\"\n";
$sXML .= "\"http://www.example.org/example.dtd\" [\n";
$sXML .= "<!ENTITY % nestedentity SYSTEM ";
$sXML .= "\"http://www.example.org/nestedentity.dtd\">\n";
$sXML .= "%nestedentity;\n";
$sXML .= "]>\n";
当前(所需)$sXML 输出:
<!DOCTYPE Example PUBLIC "urn:example:example.org:20110823:Example"
"http://www.example.org/example.dtd" [
<!ENTITY % anentity SYSTEM "http://www.example.org/nestedentity.dtd">
%anentity;
]>
当前 XMLWriter $sXML 输出(代码如下):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Example
PUBLIC "urn:example:example.org:20110823:Example"
"http://www.example.org/example.dtd" [
<!ENTITY % anentity PUBLIC "" "http://www.example.org/nestedentity.dtd">
]>
可以看到,当前的 XMLWriter 代码 XML 输出存在以下问题:
- 嵌套实体引用为 PUBLIC,而不是 SYSTEM
- 在所需的 SYSTEM 标识符之前有一个空字符串
- 在关闭 DOCTYPE 声明之前,不内联实体扩展字符串“%anentity;”。
所以,问题是,我该如何调用$oXMLWriter->writeDtdEntity
才能显示“当前(所需)$sXML
输出”部分中显示的 XML 字符串(忽略空格中的差异)?
当前 XMLWriter 代码:
<?php
$oWriter = new XMLWriter();
$oWriter->openMemory();
$oWriter->setIndent(true);
$oWriter->setIndentString("\t");
$oWriter->startDocument("1.0", "UTF-8");
$oWriter->startDtd('Example','urn:example:example.org:20110823:Example', 'http://www.example.org/example.dtd');
$oWriter->writeDtdEntity(
'nestedentity',
'%nestedentity;\n',
true,
null,
'http://www.example.org/nestedentity.dtd'
);
$oWriter->endDtd();
$oWriter->endDocument();
$sXML = $oWriter->outputMemory();