我想使用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输出存在以下问题:
所以,问题是,如何调用$oXMLWriter->writeDtdEntity
以显示“当前(所需)$sXML
输出”部分中显示的XML字符串(忽略纯空格中的差异)?< / p>
当前的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();
答案 0 :(得分:2)
嗯,我不是DTD的专家,但我注意到了一些错误:
您在XMLWriter::startDTD
中为$publicId
和$systemId
提供了值。您必须为NULL
提供$publicId
才能看到SYSTEM
而不是PUBLIC
。
您将XMLWriter::writeDTD
与XMLWriter::writeDTDEntity
混为一谈。后者只接受两个参数:实体名称和实体值
只是一个简短的例子:
$oWriter = new XMLWriter();
$oWriter->openMemory();
$oWriter->setIndent(true);
$oWriter->setIndentString("\t");
$oWriter->startDocument("1.0", "UTF-8");
// use null for $publicID to force SYSTEM
$oWriter->startDtd('Example', null, 'http://www.example.org/example.dtd');
$oWriter->writeDTDEntity('foo', 'bar');
$oWriter->endDtd();
$oWriter->endDocument();
$sXML = $oWriter->outputMemory();
结果符合预期(请注意SYSTEM
而不是PUBLIC
):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Example
SYSTEM "http://www.example.org/example.dtd" [
<!ENTITY foo "bar">
]>