首页 > Python生成XML,如何添加<mobile:mobile type="mobile"/>这种标签

Python生成XML,如何添加<mobile:mobile type="mobile"/>这种标签

<?xml version="1.0" encoding="UTF-8" ?> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/"> 
<url> 
<loc>http://m.example.com/index.html</loc> 
<mobile:mobile type="mobile"/>
<lastmod>2009-12-14</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url> 
</urlset>
xml.startElement('mobile:mobile', {'type':'pc,mobile'})

这行代码添加出来的不带/结尾,xml解析会出错


看上去和我之前写mobile的sitemap好像,似乎不需要type="mobile"吧

https://github.com/phodal/phodaldev/blob/master/templates/sitemap_mobile.xml

XML里面必须要以/结尾

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">
{% spaceless %}
{% for url in urlset %}
  <url>
    <loc>{{ url.location }}</loc>
    {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %}
    {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
    {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
    <mobile:mobile/>
   </url>
{% endfor %}
{% endspaceless %}
</urlset>
【热门文章】
【热门文章】