1

我从来没有使用过 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)从所需的输出中可以看出,我希望元素的 A 结果<homeaway>显示文本“Away @”,我希望 H 标签显示文本“Home vs”

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

4

1 回答 1

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(实际上它是一个通用的端块或右大括号)
于 2012-08-08T14:46:23.600 回答