简体   繁体   中英

How to insert text in the parent node just before a child node in PHP DomDocument

I have this XML:

<p>
     This is a test <xref>1</xref> in the XML <xref>2</xref> bla bla bla....
</p>

What I would like to do is to add the letter "N" just before the <xref>

For that, I tried to use DOMNode::insertBefore but without success.

Ex:

$refs = $paragraph->getElementsByTagName("xref");

foreach ($refs as $key=>$ref) {
   if ($key == 0) {
      $N = $dom_input->createTextNode("N");
      $citation->insertBefore($N);
   }
}

But the N text node is not inserted.

Thank you in advance for your help.

I was able to fix the problem. I did a mistake, the code should look like:

if ($key == 0) {
    $squareBracket = $dom_input->createTextNode("[");
    $paragraph->insertBefore($squareBracket, $citation);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM