如何省略 XML 命名空间声明?

How to omit XML namespace declarations?

我正在努力 DomDocument 来构建我的 XML。我需要输出这个 header:

<p:FatturaElettronica versione="FPR12" xsi:schemaLocation="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd">

为了达到这个结果我写了这段代码:

$dom  = new \DomDocument("1.0", "UTF-8");
        $init = $dom->createElementNS('http://www.example.com/XFoo', 'p:FatturaElettronica');
        $init->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
        $init->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation', 'http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd');
        $version = "FPR12";
        $init->appendChild($dom->createAttribute('versione'))->appendChild($dom->createTextNode($version));

但输出是这样的:

    <p:FatturaElettronica xmlns:p="http://www.example.com/XFoo"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://ivaservizi.agenziaentrate.gov.it/docs/xsd
/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione
/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd" 
versione="FPR12">

我哪里错了?

Where am I wrong?

是你的目标错了。如果使用名称空间前缀,则必须声明。 您目标中的 XML 使用名称空间前缀但从不声明它们。这将使 XML 而不是 namespace-well-formed

您正在生成的输出 命名空间格式正确的,并且是您 应该生产。