我想创建一个xml保存文件的标题 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd“> ,但收到错误消息
$uru = "urlset xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'";
$xml = new DOMDocument("1.0");
$xml->formatOutput=true;
$urlset=$xml->createElement("$uru");
$xml->appendChild($urlset);
结束错误是这个
PHP Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in /blablabla/blabla/file.php:11
Stack trace:
#0 /blablabla/blabla/file.php(11): DOMDocument->createElement('urlset xsi:sche...')
#1 {main}
thrown in /blablabla/blabla/file.php on line 11
THX
答案 0 :(得分:3)
你应该使用
$urlset->setAttribute
添加属性。
$urlset->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
你还需要一个元素来附加它。
以下是代码应如何显示的示例
$xml = new DOMDocument("1.0");
$urlset = $xml->createElement('SetURL');
$urlset->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
$xml->appendChild($urlset);
一点sittinote:
You should also set your xmlns and xmlns:xsi and not only the xsi:schemaLocation.
在使用站点地图时,您应该设置theese属性。
$urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
这适用于XMLNS(here is the source)
$urlset->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
对于XMLNS:XSI(Same source)
答案 1 :(得分:0)
我会尝试这种方法:
$xml = new SimpleXMLElement('<xml/>');
$uru = "urlset xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'";
$var = $xml->addChild($uru);
Header('Content-type: text/xml');
print($xml->asXML());