Milos Cuculovic
Milos Cuculovic

Reputation: 20223

How to set attribute to a DOMDocumentFragment in PHP

I have a DOMDocumentFragment - http://php.net/manual/en/class.domdocumentfragment.php that I will append as child to a DOMNode:

$fragment = $dom_output->createDocumentFragment();

$fragment->appendXML($myXML);

$cit_node->appendChild($fragment);

Everything works fine here, but I would like to add an attribute to the DOMDocumentFragment.

I can't find how to do it. Thanks for your help.

Upvotes: 1

Views: 386

Answers (1)

Milos Cuculovic
Milos Cuculovic

Reputation: 20223

I finished by doing this way:

$fragment = $dom_output->createDocumentFragment();

$fragment->appendXML($myXML);

$cit_node->appendChild($fragment);

// Find the fragment node   
$doiNode = $cit_node->getElementsByTagName('pub-id')->item(0);
// Set the attribute to the found node  
$doiNode->setAttribute('pub-id-type', 'doi');

As mentioned by @hakre, it is not possible to add an attribute to a frgment, but adding the attribute to the element solved my problem.

Upvotes: 1

Related Questions