xmlstarlet,根据子标记在XML中查找标题

时间:2017-09-29 22:10:51

标签: linux xmlstarlet

我有以下XML:

 <rss version="2.0"
    xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:wp="http://wordpress.org/export/1.2/"
>
<channel>    
     <item>
        <title>Lucy – Official trailer 2014 – Universal Pictures</title>
        <pubDate>Mon, 10 Jul 2017 13:13:05 +0000</pubDate>
        <description></description>
        <excerpt:encoded><![CDATA[]]></excerpt:encoded>
        <wp:post_id>5688</wp:post_id>
        <wp:post_date><![CDATA[2017-07-10 13:13:05]]></wp:post_date>
        <wp:post_date_gmt><![CDATA[2017-07-10 13:13:05]]></wp:post_date_gmt>
        <wp:comment_status><![CDATA[closed]]></wp:comment_status>
        <wp:ping_status><![CDATA[open]]></wp:ping_status>
        <wp:post_name><![CDATA[lucy-official-trailer-2014-universal-pictures]]></wp:post_name>
        <wp:status><![CDATA[publish]]></wp:status>
        <wp:post_parent>0</wp:post_parent>
        <wp:menu_order>0</wp:menu_order>
        <wp:post_type><![CDATA[post]]></wp:post_type>
        <wp:post_password><![CDATA[]]></wp:post_password>
        <wp:is_sticky>0</wp:is_sticky>
        <wp:postmeta>
            <wp:meta_key><![CDATA[jtheme_video_file]]></wp:meta_key>
            <wp:meta_value><![CDATA[]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key><![CDATA[_post_like_count]]></wp:meta_key>
            <wp:meta_value><![CDATA[6]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key><![CDATA[snap_isAutoPosted]]></wp:meta_key>
            <wp:meta_value><![CDATA[1]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key><![CDATA[_snap_forceSURL]]></wp:meta_key>
            <wp:meta_value><![CDATA[2]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key><![CDATA[snap_MYURL]]></wp:meta_key>
            <wp:meta_value><![CDATA[]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key><![CDATA[snapEdIT]]></wp:meta_key>
            <wp:meta_value><![CDATA[1]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key><![CDATA[_post_like_modified]]></wp:meta_key>
            <wp:meta_value><![CDATA[2017-07-13 19:58:16]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key><![CDATA[_yst_prominent_words_version]]></wp:meta_key>
            <wp:meta_value><![CDATA[1]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key><![CDATA[jtheme_video_code]]></wp:meta_key>
            <wp:meta_value><![CDATA[<iframe width="1280" height="720" src="https://www.youtube.com/embed/bN7ksFEVO9U" frameborder="0" allowfullscreen></iframe>]]></wp:meta_value>
        </wp:postmeta>
    </item>
</channel>

通过使用xmlstarlet和XPath,我想搜索wp:postmeta,它的标签为wp:meta_value,带有videoID bN7ksFEVO9U。

找到正确的wp:metavalue后应打印出该项目下标签的标题

提前谢谢

1 个答案:

答案 0 :(得分:1)

您需要做的是将http://wordpress.org/export/1.2/命名空间绑定到一个前缀(使用-N),匹配正确的item(使用-m)并打印值(使用-v)。您还可以使用-n在标题后打印换行符。

示例...

==> xml sel -N wp=http://wordpress.org/export/1.2/ -t -m "/rss/channel/item[wp:postmeta[normalize-space(wp:meta_key)='jtheme_video_code' and contains(wp:meta_value,'/bN7ksFEVO9U\"')]]" -v "title" -n input.xml
Lucy – Official trailer 2014 – Universal Pictures