PHP XMLWriter:我需要哪些参数来传递两个XMLWriter :: write IdEntity来生成如上所述的嵌套实体DTD声明?

时间:2011-08-23 18:15:28

标签: php xml dtd xmlwriter

我想使用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声明之前,
  4. 没有内联实体扩展字符串'%anentity;'。
  5. 所以,问题是,如何调用$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();
    

1 个答案:

答案 0 :(得分: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">
]>