您似乎无法使用simpleXML
定义
将在文档中使用的命名空间(即
xmlns
属性可以)。我发现您可以在根节点的声明中简单地指定它们,如下所示:
$simpleXml = new SimpleXMLElement('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"></urlset>');
在构建站点地图的情况下,您可能不必担心设置任何名称空间。但是,对于更通用的解决方案,它定义了一个默认名称空间和一个带前缀的第二个名称空间。
alt
.
$simpleXml = new SimpleXMLElement('<root xmlns="http://default.namespace.com" xmlns:alt="http://alt.namespace.com"></root>');
$simpleXml->addChild("child", "node in the default namespace");
$simpleXml->addChild("other", "node in the alternate namespace", "http://alt.namespace.com");
print $simpleXml->asXML();
将产生:
<root xmlns="http://default.namespace.com" xmlns:alt="http://alt.namespace.com">
<child>node in the default namespace</child>
<alt:other>node in the alternate namespace</alt:other>
</root>
第三个可选参数
addAttribute
可以帮助您使用该名称空间创建属性或节点的命名空间。请注意,您需要使用
网址
名称空间(而不是前缀)。