2

I would like to extract only a value from similar content XML file.

Example;

<?xml version="1.0" encoding="UTF-8" ?>
<items>   

<channel>
<title><![CDATA[*** text 1 text ***]]></title>
<playlist_url><![CDATA[http://host.net/aa/15_info/]]></playlist_url>
</channel>

<channel>       
<title><![CDATA[*** text 2 text ***]]></title>
<playlist_url><![CDATA[http://host.net/aa/16_info/]]></playlist_url>
</channel>

<channel>      
<title><![CDATA[*** text 3 text ***]]></title>
<playlist_url><![CDATA[http://host.net/aa/vodpr/]]></playlist_url>  
<protected>True</protected> 
</channel>

<channel>
<title><![CDATA[*** text 4 text ***]]></title>
<playlist_url><![CDATA[http://host.net/aa/vodpr/con_tv_r.php]]></playlist_url>
<protected>True</protected> 
</channel>

</items>

I need extract seperatly 'http://host.net/aa/vodpr/' and 'http://host.net/aa/vodpr/con_tv_t.php' variable url values.

Best Regards

1 Answer 1

2

Assuming you want to get the value of the playlist_url nodes for each channel node that has a protected node with value True:

$ xmlstarlet sel -t -v '//channel[protected = "True"]/playlist_url' -nl file.xml
http://host.net/aa/vodpr/
http://host.net/aa/vodpr/con_tv_r.php

This uses xmlstarlet to apply an XPATH query to the document. The -nl at the end adds a terminating newline to the last piece of data.


To select the URL that corresponds to the specific text in the title node (for example, "contains text 3"), use

$ xmlstarlet sel -t -v '//channel[./title[contains(., "text 3")]]/playlist_url' -nl file.xml
http://host.net/aa/vodpr/

Here we detect the text 3 text in the value of the title node and select that particular channel node. We then pick the playlist_url from it.

6

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .