PHP DOMDocument insertBefore抛出“未找到错误”

I have a DOMDocument and I am trying to insert an node before an other.

$doc = $parent->ownerDocument;
$xref_node = $doc->importNode($xref, true);
$ref = $doc->importNode($ref, true);
$parent->insertBefore($xref_node, $ref_node);

$parent, $xref and $ref are DOMElements.

Somehow, this is generating an "Not Found Error" on the line where I am using insertBefore.

Any idea why? I am using here the ownerDocument of the $parent, importing the two nodes $xref and $ref in this document and then try to use the insert before, on nodes from the same document.

Thank you in advance.

There is a bug: $ref = should be $ref_node =