【问题标题】:generating sitemap with DOMDocument : missing AttributeNode in the output使用 DOMDocument 生成站点地图:输出中缺少 AttributeNode
【发布时间】:2022-01-03 21:34:45
【问题描述】:

我正在尝试生成 sitemap.xml,这是我的代码的简化版本

    $dom = new \DOMDocument();
    $dom->encoding = 'utf-8';
    $dom->xmlVersion = '1.0';
    $dom->formatOutput = true;
    $xml_file_name = './sitemap.xml';
    $urlset = $dom->createElement('urlset');

    $attr_ = new \DOMAttr('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
    $urlset->setAttributeNode($attr_);

    $url_node = $dom->createElement('url');

    $url_node_loc = $dom->createElement('loc',   'http://localhost' );
    $url_node->appendChild($url_node_loc);

    $url_node_lastmod = $dom->createElement('lastmod',  '2021-08-03T22:17:47+04:30' );
    $url_node->appendChild($url_node_lastmod);

    $urlset->appendChild($url_node);

    $dom->appendChild($urlset);
    $dom->save($xml_file_name);
    dd('done');

这是我的 sitemap.xml 中的输出

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<urlset>
    <url>
        <loc>http://localhost</loc>
        <lastmod>2021-08-03T22:17:47+04:30</lastmod>
    </url>
</urlset>

我需要为我的urlset 标签添加一些属性,这就是我的做法

    $attr_ = new \DOMAttr('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
    $urlset->setAttributeNode($attr_);

但由于某种原因,这没有出现在我的站点地图文件中,urlset 没有属性

【问题讨论】:

    标签: php xml domdocument


    【解决方案1】:

    使用setAttribute() 而不是setAttributeNode()

    $urlset->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
    

    【讨论】:

      【解决方案2】:

      这不是有效的站点地图。站点地图使用 XML 命名空间 (https://www.sitemaps.org/protocol.html)

      要使用命名空间创建节点,您应该使用带有 *NS 后缀的命名空间感知 DOM 方法。这将根据需要添加命名空间定义。

      xmlns:xsi 是一个命名空间定义。它们可以被视为保留命名空间 {http://www.w3.org/2000/xmlns/} 中的属性节点。

      $xmlns = [
        'sitemap' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
        'xmlns' => 'http://www.w3.org/2000/xmlns/',  
        'xsi' => 'http://www.w3.org/2001/XMLSchema-instance',  
      ];
      
      $document = new \DOMDocument('1.0', 'utf-8');
      $document->formatOutput = true;
          
      $urlset = $document->appendChild(
          $document->createElementNS($xmlns['sitemap'], 'urlset')
      );
      // explict namespace definition
      $urlset->setAttributeNS(
          $xmlns['xmlns'], 'xmlns:xsi', $xmlns['xsi']
      );
      
      $url_node = $urlset->appendChild(
        $document->createElementNS($xmlns['sitemap'], 'url')
      );
      
      $url_node
        ->appendChild($document->createElementNS($xmlns['sitemap'], 'loc'))
        ->textContent = 'http://localhost';
      $url_node
        ->appendChild($document->createElementNS($xmlns['sitemap'], 'lastmod'))
        ->textContent = '2021-08-03T22:17:47+04:30';
        
      echo $document->saveXML();  
      

      输出:

      <?xml version="1.0" encoding="utf-8"?>
      <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <url>
          <loc>http://localhost</loc>
          <lastmod>2021-08-03T22:17:47+04:30</lastmod>
        </url>
      </urlset>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-29
        • 2020-04-09
        • 1970-01-01
        • 1970-01-01
        • 2015-08-11
        • 1970-01-01
        • 2015-01-31
        • 1970-01-01
        相关资源
        最近更新 更多