Search code examples
bashvariablescurlxmlstarlet

bash - xmlstarlet -v to $variable


wget ${RSS_URL} -O - 2>/dev/null | xmlstarlet sel -t -m "/rss/channel/item" -v "title" -n -v "link" -n -n

That line prints out the title (-v "title) and the url (-v "link") of entries in a rss feed I have. I want to store the title and url into separate variables ($title and $url for example) so I can use them for curl later.

How can I do this?


Solution

  • Run xmlstarlet twice, once for each information you want to get.

    To make this efficient, you should download the URL to a local file (so only once wget) and then run xmlstarlet on that file:

    title=$(xmlstarlet sel -t -m "/rss/channel/item" -v "title" local.xml)
    link=$(xmlstarlet sel -t -m "/rss/channel/item" -v "link" local.xml)
    

    I suggest this approach because it will play well with whitespace in the elements and even new lines.

    Note: To optimize the process, you can first filter the XML input to produce a new XML file which just contains the information you want and then run xmlstarlet again on that to split it into variables.