Xmlstarlet,多个元素和日期排除

时间:2012-08-06 03:01:41

标签: xml linux bash terminal xmlstarlet

我从未使用过xmlstarlet,所以我只是想让输入和所需输出保持简单,希望我可以从发布的答案中学到一些东西。

输入:

    

<channel>
    <title>
    <link>http://www.MYDOMAIN.com/</link>
    <description></description>
    <ttl>60</ttl>
    <category>Sports</category>

        <item>
             <sport><![CDATA[Women's Soccer]]></sport>
             <opponent><![CDATA[South Carolina (Exhibition)]]></opponent>
             <location><![CDATA[Columbia, S.C.]]></location>
             <date>08/09/2012</date>
             <time>7:00 PM</time>
             <tv><![CDATA[]]></tv>
             <tournamentname><![CDATA[]]></tournamentname>
             <homeaway>A</homeaway>
        </item>     

        <item>
             <sport><![CDATA[Men's Soccer]]></sport>
             <opponent><![CDATA[Ohio State (Exhibition)]]></opponent>
             <location><![CDATA[Fort Wayne, Ind.]]></location>
             <date>08/17/2012</date>
             <time>7:00 PM</time>
             <tv><![CDATA[]]></tv>
             <tournamentname><![CDATA[]]></tournamentname>
             <homeaway>H</homeaway>
        </item>     

        <item>
             <sport><![CDATA[Women's Soccer]]></sport>
             <opponent><![CDATA[William and Mary]]></opponent>
             <location><![CDATA[Williamsburg, Va.]]></location>
             <date>08/17/2012</date>
             <time>7:00 PM</time>
             <tv><![CDATA[]]></tv>
             <tournamentname><![CDATA[]]></tournamentname>
             <homeaway>A</homeaway>
        </item>

期望的输出:

Women's Soccer Away @ South Carolina (Exhibition) at 7:00 PM
Men's Soccer Home vs Ohio State at 7:00 PM
Women's Soccer Away @ William and Mary at 7:00 PM

一些使这不仅仅是简单的数据操作的东西

1)我想删除所有没有<date>当前日期的数据

2)正如您从所需的输出中看到的那样,我希望元素<homeaway>的结果显示文本'Away @',并且我希望H标签显示文字'Home vs'

非常感谢你的帮助,我真的很感激。

1 个答案:

答案 0 :(得分:4)

xmlstarlet sel --text -t \
    -m '//channel[category="Sports"]/item[date="'$(date +%m/%d/%Y)'"]' \
    -v sport -o ' ' \
    --if 'homeaway="H"' -o 'Home vs' --else -o 'Away @' -b -o ' ' \
    -v opponent -o ' ' \
    -o 'at ' -v time --nl \
    /tmp/rss.xml
  • -m(或--match)启动foreach循环
  • -b(或--break)表示endif(实际上它是一般的结束块或右括号)