3

我想使用 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 输出存在以下问题:

  1. 嵌套实体引用为 PUBLIC,而不是 SYSTEM
  2. 在所需的 SYSTEM 标识符之前有一个空字符串
  3. 在关闭 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();
4

1 回答 1

2

好吧,我不是 DTD 方面的专家,但我注意到几个错误:

只是一个简短的例子:

$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">
]>
于 2013-11-18T12:21:03.873 回答