DOMDocument,创建具有多个关联命名空间的新元素节点

DOMDocument, Create new element node with several associated namespace

正在尝试使用 DOMDocument 创建此结构。

<?xml version="1.0" encoding="UTF-8"?>

  <xbrli:xbrl
      xmlns:link        = "http://www.xbrl.org/2003/linkbase"
      xmlns:se-gen-base = "http://www.taxonomier.se/se/fr/gen-base/2017-09-30"
      xmlns:iso4217     = "http://www.xbrl.org/2003/iso4217"
      xmlns:xlink       = "http://www.w3.org/1999/xlink"
      xmlns:xbrli       = "http://www.xbrl.org/2003/instance"
      xmlns:se-cd-base  = "http://www.taxonomier.se/se/fr/cd-base/2017-09-30"
  >

</xbrli:xbrl>

我现在的代码:

$xml = new DomDocument('1.0', 'utf-8');
$xml->formatOutput = true;

$element = $xml->createElementNS('http://www.example.com/XFoo', 'xbrli:xbrl');
$xml->appendChild($element);

echo $xml->saveXML();

结果:

<?xml version="1.0" encoding="utf-8"?>
<xbrli:xbrl xmlns:xbrli="http://www.xbrl.org/2003/linkbase"/>

您可以添加额外的命名空间作为标准属性:

$xml = new DomDocument('1.0', 'utf-8');
$xml->formatOutput = true;

$element = $xml->createElement('xbrli:xbrl');

$namespaceList = array(
    'xmlns:link'        => "http://www.xbrl.org/2003/linkbase",
    'xmlns:se-gen-base' => "http://www.taxonomier.se/se/fr/gen-base/2017-09-30",
    'xmlns:iso4217'     => "http://www.xbrl.org/2003/iso4217",
    'xmlns:xlink'       => "http://www.w3.org/1999/xlink",
    'xmlns:xbrli'       => "http://www.xbrl.org/2003/instance",
    'xmlns:se-cd-base'  => "http://www.taxonomier.se/se/fr/cd-base/2017-09-30",
)
;

foreach($namespaceList as $key => $uri){
    $attr = $xml->createAttribute($key);
    $attr->value =  $uri;
    $element->appendChild($attr);
}

$xml->appendChild($element);

echo $xml->saveXML();

输出:

<?xml version="1.0" encoding="utf-8"?>
<xbrli:xbrl xmlns:link="http://www.xbrl.org/2003/linkbase" xmlns:se-gen-base="http://www.taxonomier.se/se/fr/gen-base/2017-09-30" xmlns:iso4217="http://www.xbrl.org/2003/iso4217" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xbrli="http://www.xbrl.org/2003/instance" xmlns:se-cd-base="http://www.taxonomier.se/se/fr/cd-base/2017-09-30"/>

你走在正确的轨道上。如果你使用命名空间,你应该使用命名空间感知方法(后缀 NS)。这将隐式添加命名空间定义(在使用时)。您仍然可以通过将它们设置为属性来明确定义它们:

$document = new DOMDocument('1.0', 'UTF-8');
$document->formatOutput = true;

// append returns the node, the create call can be nested
$element = $document->appendChild(
    // use the namespace aware method - this will add the namespace definition as needed
    $document->createElementNS('http://www.xbrl.org/2003/instance', 'xbrli:xbrl')
);

$namespaces = [
    'link' => "http://www.xbrl.org/2003/linkbase",
    'se-gen-base' => "http://www.taxonomier.se/se/fr/gen-base/2017-09-30",
    'iso4217' => "http://www.xbrl.org/2003/iso4217",
    'xlink' => "http://www.w3.org/1999/xlink",
    // 'xbrli' => "http://www.xbrl.org/2003/instance",
    'se-cd-base' => "http://www.taxonomier.se/se/fr/cd-base/2017-09-30",
];

foreach ($namespaces as $prefix => $namespaceURI) {
    // namespace definitions use a resevered namespace
    $element->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:'.$prefix, $namespaceURI);
};

echo $document->saveXML();

输出:

<?xml version="1.0" encoding="UTF-8"?>
<xbrli:xbrl xmlns:xbrli="http://www.xbrl.org/2003/instance" xmlns:link="http://www.xbrl.org/2003/linkbase" xmlns:se-gen-base="http://www.taxonomier.se/se/fr/gen-base/2017-09-30" xmlns:iso4217="http://www.xbrl.org/2003/iso4217" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:se-cd-base="http://www.taxonomier.se/se/fr/cd-base/2017-09-30"/>