简体   繁体   English

PHP DOM Document:如何在xml中将节点“default:link”替换或更改为“xhtml:link”?

[英]PHP DOM Document: How to replace or change the node “default:link” into “xhtml:link” in the xml?

I tried to remove the attribute "xmlns:xhtml" from the tag "xhtml:link" with the following code: 我尝试使用以下代码从标记“xhtml:link”中删除属性“xmlns:xhtml”:

Source Code: 源代码:

    $doc = new DOMDocument('1.0', 'utf-8');
    $url = 'android-app://com.domain.name';
    $element = $doc->createElementNS($url,'xhtml:link');

    $attribute = $doc->childNodes->item(0);

    //echo '<br>tag: '.$doc->getElementsByTagName("xhtml:link")[0];

    $element->setAttribute('href', $url);
    $element->setAttribute('rel', 'alternate');

    //echo '<pre>';print_r($element);echo '</pre>';


    $element->hasAttributeNS($url, 'xhtml');
    $element->removeAttributeNS($url, 'xhtml');

    $doc->appendChild($element);
    echo $doc->saveXML();

OutPut: 输出:

    <?xml version="1.0" encoding="utf-8"?>
    <default:link href="android-app://com.domain.name" rel="alternate"/>

But, I am expecting the output looks like: 但是,我期待输出看起来像:

    <?xml version="1.0" encoding="utf-8"?>
    <xhtml:link href="android-app://com.domain.name" rel="alternate"/>

Please help me what I have to do? 请帮帮我,我要做什么? Here I struck to replace the tag... 在这里我敲响了替换标签......

Thanks! 谢谢!

Try createElement function instead of createElementNS : 尝试使用createElement函数而不是createElementNS

$doc = new DOMDocument('1.0', 'utf-8');
$url = 'android-app://com.domain.name';
$element = $doc->createElement('xhtml:link');

$attribute = $doc->childNodes->item(0);

$element->setAttribute('href', $url);
$element->setAttribute('rel', 'alternate');

$doc->appendChild($element);
echo $doc->saveXML();

OUTPUT: OUTPUT:

<?xml version="1.0" encoding="utf-8"?>
<xhtml:link href="android-app://com.domain.name" rel="alternate"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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