[英]How to insert text in the parent node just before a child node in PHP DomDocument
I have this XML: 我有这个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>
我想做的是在<xref>
之前添加字母“ N”
For that, I tried to use DOMNode::insertBefore
but without success. 为此,我尝试使用DOMNode::insertBefore
但没有成功。
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. 但是未插入N文本节点。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.