1

Trying to create this structure, using 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>

My current code:

$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();

Result:

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

2 Answers 2

1

You can add additional namespaces as standard attributes :

$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();

Output :

<?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"/>
Sign up to request clarification or add additional context in comments.

1 Comment

It should be OK now
0

You're on the right track. If you work with namespaces, you should use the namespace aware methods (suffix NS). This will add namespace definitions implicitly (on use). You can still define them explicitly by setting them as an attribute:

$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();

Output:

<?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"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.