如何使用 PHP 和 DOMDocument 生成包含页面本地化版本的 Google 站点地图
How to generate a Google Sitemap with localized versions of a page using PHP and DOMDocument
我想使用 PHP 为多语言网站生成 Google 站点地图。我怎样才能以正确的方式做到这一点,而不打印 XML 字符串?也许使用 PHP DOMDocument class?
结构必须像 Google 推荐的那样:https://support.google.com/webmasters/answer/189077#sitemap
这个问题我已经有了答案:
$domtree = new DOMDocument("1.0", "UTF-8");
$domtree->formatOutput = true;
$urlset = $domtree->createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9", "urlset");
$domtree->appendChild($urlset);
$urlset->setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xhtml", "http://www.w3.org/1999/xhtml");
{
//create the `url`
$url = $domtree->createElement("url");
//add the url
$url_root = $urlset->appendChild($url);
{
//create the `loc`
$loc = $domtree->createElement("loc", "http://www.example.com/english/page.html");
//add the loc to the url
$url_root->appendChild($loc);
//create `xhtml:link`
$xhtml_link = $domtree->createElement("xhtml:link");
//add the `xhtml:link` to the `url`
$url->appendChild($xhtml_link);
{
//create an `rel` attribute to the `xhtml:link`
$dom_attribute = $domtree->createAttribute("rel");
$dom_attribute->value="alternate";
//add the attribute to the `xhtml:link`
$xhtml_link->appendChild($dom_attribute);
//create an `hreflang` attribute to the `xhtml:link`
$dom_attribute = $domtree->createAttribute("hreflang");
$dom_attribute->value="en";
//add the attribute to the `xhtml:link`
$xhtml_link->appendChild($dom_attribute);
//create an `href` attribute to the `xhtml:link`
$dom_attribute = $domtree->createAttribute("href");
$dom_attribute->value="http://www.example.com/english/page.html";
//add the attribute to the `xhtml:link`
$xhtml_link->appendChild($dom_attribute);
}
}
}
我想使用 PHP 为多语言网站生成 Google 站点地图。我怎样才能以正确的方式做到这一点,而不打印 XML 字符串?也许使用 PHP DOMDocument class?
结构必须像 Google 推荐的那样:https://support.google.com/webmasters/answer/189077#sitemap
这个问题我已经有了答案:
$domtree = new DOMDocument("1.0", "UTF-8");
$domtree->formatOutput = true;
$urlset = $domtree->createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9", "urlset");
$domtree->appendChild($urlset);
$urlset->setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xhtml", "http://www.w3.org/1999/xhtml");
{
//create the `url`
$url = $domtree->createElement("url");
//add the url
$url_root = $urlset->appendChild($url);
{
//create the `loc`
$loc = $domtree->createElement("loc", "http://www.example.com/english/page.html");
//add the loc to the url
$url_root->appendChild($loc);
//create `xhtml:link`
$xhtml_link = $domtree->createElement("xhtml:link");
//add the `xhtml:link` to the `url`
$url->appendChild($xhtml_link);
{
//create an `rel` attribute to the `xhtml:link`
$dom_attribute = $domtree->createAttribute("rel");
$dom_attribute->value="alternate";
//add the attribute to the `xhtml:link`
$xhtml_link->appendChild($dom_attribute);
//create an `hreflang` attribute to the `xhtml:link`
$dom_attribute = $domtree->createAttribute("hreflang");
$dom_attribute->value="en";
//add the attribute to the `xhtml:link`
$xhtml_link->appendChild($dom_attribute);
//create an `href` attribute to the `xhtml:link`
$dom_attribute = $domtree->createAttribute("href");
$dom_attribute->value="http://www.example.com/english/page.html";
//add the attribute to the `xhtml:link`
$xhtml_link->appendChild($dom_attribute);
}
}
}