1

次のコードがありますが、適切なサイトマップ標準が必要です。

私のコード

$xml = new DomDocument('1.0', 'utf-8'); 
$xml->formatOutput = true; 
$products= $xml->createElement('url'); 
$product = $xml->createElement('url'); 
$xml->appendChild($products); 
$products->appendChild($product); 
$product->appendChild(new DomAttr('loc', '123')); 
$xml->save("data.xml");

しかし、記載されたコードの出力はlileです:

<?xml version="1.0" encoding="utf-8"?>
<url>
<url loc="123"/>
</url>

しかし、私は次の基準が欲しい...

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>http://www.example.com/</loc>
      <lastmod>2012-08-11T04:08:53+01:00</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.5</priority>
   </url>
</urlset>

この点で私を助けてください。

4

1 に答える 1

5

構文によって XML ドキュメントを作成できることは既に証明されています。標準のテンプレートを再作成するだけです。

例として、Web サイトの情報が次のような配列に格納されているとします。

$yourSiteContent = array(
    array('permalink' => 'http://www.somesite.com/someurl1', 'updated' => '2012-08-11T04:08:53+01:00'),
    array('permalink' => 'http://www.somesite.com/someurl2', 'updated' => '2012-09-11T04:08:53+01:00'),
    array('permalink' => 'http://www.somesite.com/someurl3', 'updated' => '2012-10-11T04:08:53+01:00')
);

次に、例に戻ります。

$xml = new DomDocument('1.0', 'utf-8'); 
$xml->formatOutput = true; 

// creating base node
$urlset = $xml->createElement('urlset'); 
$urlset -> appendChild(
    new DomAttr('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')
);

    // appending it to document
$xml -> appendChild($urlset);

// building the xml document with your website content
foreach($yourSiteContent as $entry)
{

    //Creating single url node
    $url = $xml->createElement('url'); 

    //Filling node with entry info
    $url -> appendChild( $xml->createElement('loc', $entry['permalink']) ); 
    $url -> appendChild( $lastmod = $xml->createElement('lastmod', $entry['updated']) ); 
    $url -> appendChild( $changefreq = $xml->createElement('changefreq', 'monthly') ); 
    $url -> appendChild( $priority = $xml->createElement('priority', '0.5') ); 

    // append url to urlset node
    $urlset -> appendChild($url);

}

$xml->save("data.xml");

後は君しだい。

于 2012-08-23T01:53:33.110 に答える