XMLStarlet - 添加RSS提要项

时间:2014-08-07 21:00:08

标签: rss xmlstarlet

使用我在这里找到的一个例子(https://stackoverflow.com/a/14397390/3168446),我发现它没有正确添加项目。以下示例实际上是在channel-tag之外添加项目。有人知道正确的方法吗?

feed.xml:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>My RSS Feed</title>
    <description>This is my RSS Feed</description>
  </channel>
</rss>

shell脚本:

#!/bin/sh

TITLE="My RSS entry"
LINK="http://example.com/entry4711"
DATE="`date`"
DESC="Good news"
GUID="http://example.com/entry4711" 

xmlstarlet ed -L   -a "//channel" -t elem -n item -v ""  \
     -s "//item[1]" -t elem -n title -v "$TITLE" \
     -s "//item[1]" -t elem -n link -v "$LINK" \
     -s "//item[1]" -t elem -n pubDate -v "$DATE" \
     -s "//item[1]" -t elem -n description -v "$DESC" \
     -s "//item[1]" -t elem -n guid -v "$GUID" \
     -d "//item[position()>10]"  feed.xml ; 

windows命令行示例:

xml ed -L -a "//channel" -t elem -n item -v "" -s "//item[1]" -t elem -n title -v "My RSS entry" -s "//item[1]" -t elem -n link -v "http://example.com/entry4711" -s "//item[1]" -t elem -n pubDate -v "Sat, 26 Jul 2014 01:14:30 +0200" -s "//item[1]" -t elem -n description -v "Good news" -s "//item[1]" -t elem -n guid -v "http://example.com/entry4711" -d "//item[position()>10]" feed.xml

输出feed.xml:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>My RSS Feed</title>
    <description>This is my RSS Feed</description>
  </channel>
  <item>
    <title>My RSS entry</title>
    <link>http://example.com/entry4711</link>
    <pubDate>Sat, 26 Jul 2014 01:14:30 +0200</pubDate>
    <description>Good news</description>
    <guid>http://example.com/entry4711</guid>
  </item>
</rss>

正如您在输出中看到的那样,该项目已添加到channel-tag之外,因此Feed无法验证。

1 个答案:

答案 0 :(得分:0)

经过一夜安眠后,实际上并没有这么难。

每个项目都添加到频道标签之外的原因是因为我使用了;

xml ed -L -a "//channel"

解决方案是在Feed模板中使用未在要添加的项目中使用的标记。我正在使用generator-tag。

添加了generator-tag的示例feed.xml:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>My RSS Feed</title>
    <description>This is my RSS Feed</description>
    <link>http://example.com/entry4711</link>
    <generator>your-rss-generator</generator>
  </channel>
</rss>

shell脚本,我将// channel替换为// generator:

#!/bin/sh

TITLE="My RSS entry"
LINK="http://example.com/entry4711"
DATE="`date`"
DESC="Good news"
GUID="http://example.com/entry4711" 

xmlstarlet ed -L   -a "//generator" -t elem -n item -v ""  \
     -s "//item[1]" -t elem -n title -v "$TITLE" \
     -s "//item[1]" -t elem -n link -v "$LINK" \
     -s "//item[1]" -t elem -n pubDate -v "$DATE" \
     -s "//item[1]" -t elem -n description -v "$DESC" \
     -s "//item[1]" -t elem -n guid -v "$GUID" \
     -d "//item[position()>10]"  feed.xml ; 

Windows命令行,我将// channel替换为// generator:

xml ed -L -a "//generator" -t elem -n item -v "" -s "//item[1]" -t elem -n title -v "My RSS entry" -s "//item[1]" -t elem -n link -v "http://example.com/entry4711" -s "//item[1]" -t elem -n pubDate -v "Sat, 26 Jul 2014 01:14:30 +0200" -s "//item[1]" -t elem -n description -v "Good news" -s "//item[1]" -t elem -n guid -v "http://example.com/entry4711" -d "//item[position()>10]" feed.xml

这将使每个新项目都在频道标记内。