Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用命名空间创建SimpleXMLElement节点并防止命名空间重复_Php_Xml_Symfony_Namespaces_Simplexml - Fatal编程技术网

Php 使用命名空间创建SimpleXMLElement节点并防止命名空间重复

Php 使用命名空间创建SimpleXMLElement节点并防止命名空间重复,php,xml,symfony,namespaces,simplexml,Php,Xml,Symfony,Namespaces,Simplexml,对于google站点地图,我想创建具有名称空间的XML节点。如何防止simplexml在每个节点上插入名称空间 我需要的结构: <xhtml:link rel="alternate" hreflang="de" href="http://www.example.com/deutsch/" /> 我的代码中的结构: <?xml version="1.0" enc

对于google站点地图,我想创建具有名称空间的XML节点。如何防止simplexml在每个节点上插入名称空间

我需要的结构:

<xhtml:link 
             rel="alternate"
             hreflang="de"
             href="http://www.example.com/deutsch/"
             />

我的代码中的结构:

    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
       <url>
          <loc>www.url.ch</loc>
          <xhtml:link xmlns:xhtml="xhtml" rel="alternate" hreflang="de-CH" href="www.url.ch/de">www.url.ch/de</xhtml:link>
          <xhtml:link xmlns:xhtml="xhtml" rel="alternate" hreflang="fr-CH" href="www.url.ch/fr">www.url.ch/fr</xhtml:link>
       </url>
    </urlset>

www.url.ch
www.url.ch/de
www.url.ch/fr
我的代码:

        $rootNode = new SimpleXMLElement(
            '<?xml version="1.0" encoding="utf-8"?>' .
            '   <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"></urlset>'
        );

        $urlNode = $rootNode->addChild('url');
        $urlNode->addChild('loc', 'www.url.ch');

        foreach (['de', 'fr', 'it', 'en'] as $locale) {
            if (in_array($locale, ['it', 'en'])) {
                continue;
            }

            $localeNode = $urlNode->addChild(
                'xhtml:link',
                'www.url.ch' . '/' . $locale,
                'xhtml'
            );

            $localeNode->addAttribute('rel', 'alternate');
            $localeNode->addAttribute('hreflang', $locale . '-CH');
            $localeNode->addAttribute('href', 'www.url.ch' . '/' . $locale);
        }

        $rootNode->saveXML($filePath);
$rootNode=新的SimpleXMLElement(
'' .
'   '
);
$urlNode=$rootNode->addChild('url');
$urlNode->addChild('loc','www.url.ch');
foreach(['de','fr','it','en']作为$locale){
if(在数组中($locale,['it','en'])){
继续;
}
$localeNode=$urlNode->addChild(
'xhtml:link',
“www.url.ch.”/“$locale,
“xhtml”
);
$localeNode->addAttribute('rel','alternate');
$localeNode->addAttribute('hreflang',$locale'-CH');
$localeNode->addAttribute('href','www.url.ch'./.$locale);
}
$rootNode->saveXML($filePath);

您需要在
addChild
调用中将名称空间指定为全局唯一的“名称空间标识符”(URI),而不是“本地前缀”。因此,在本例中,您将
xhtml
前缀绑定为
xmlns:xhtml=”http://www.w3.org/1999/xhtml“
因此名称空间URI是
http://www.w3.org/1999/xhtml

$localeNode = $urlNode->addChild(
    'xhtml:link',
    'www.url.ch' . '/' . $locale,
    'http://www.w3.org/1999/xhtml'
);

然后,XML库在生成XML时查找已为此命名空间分配的前缀,并给出所需的结果。

谢谢。就这样。