r - 如何使用 R 和 {XML} 包创建 sitemap.xml 文件?

标签 r xml xml-sitemap

我有一个链接向量,我想从中创建一个 sitemap.xml 文件(文件协议(protocol)可从此处获得:http://www.sitemaps.org/protocol.html)

我了解 sitemap.xml 协议(protocol)(它相当简单),但我不确定为它使用 {XML} 包的最明智的方法是什么。

一个简单的例子:

 links <- c("http://r-statistics.com",
             "http://www.r-statistics.com/on/r/",
             "http://www.r-statistics.com/on/ubuntu/")

如何使用“链接”来构建 sitemap.xml 文件?

最佳答案

这就是您要找的东西吗? (它使用 httr 包获取最后修改的位,并使用非常有用的 whisker 包直接写入 XML。)

require(whisker)
require(httr)
tpl <- '
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
 {{#links}}
   <url>
      <loc>{{{loc}}}</loc>
      <lastmod>{{{lastmod}}}</lastmod>
      <changefreq>{{{changefreq}}}</changefreq>
      <priority>{{{priority}}}</priority>
   </url>
 {{/links}}
</urlset>
'

links <- c("http://r-statistics.com", "http://www.r-statistics.com/on/r/", "http://www.r-statistics.com/on/ubuntu/")


map_links <- function(l) {
  tmp <- GET(l)
  d <- tmp$headers[['last-modified']]

  list(loc=l,
       lastmod=format(as.Date(d,format="%a, %d %b %Y %H:%M:%S")),
       changefreq="monthly",
       priority="0.8")
}

links <- lapply(links, map_links)

cat(whisker.render(tpl))

关于r - 如何使用 R 和 {XML} 包创建 sitemap.xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12255359/

相关文章:

r - 为什么 seq() 同时创建 int 和 num 向量,而 c() 从不创建 int 向量?

iphone - 如何检查是否结束xml解析

r - 如何使用 ggplot2 删除特定值处的网格线?

r - For 循环和 Map 等价物不匹配

xml - 使用浏览器转换XML时,是否可以通过URL向XSLT传递参数?

xhtml - 为什么我收到 Sitemap.xml 的错误 “one or more unbound namespace prefixes”

xml-sitemap - 在谷歌搜索结果中显示站点地图

r - 基于其他列的 ggplot2 构面中的自定义轴中断

sql-server - XSL - GML 到 json