xmlxpathxmlstarletxq

Selecting multple values from an XML document in linux shell


XML: https://raw.githubusercontent.com/dp247/Freeview-EPG/master/epg.xml

Example

<tv>
  <programme channel="VirginRadio.uk" start="20250319220000 +0000" stop="20250320010000 +0000">
    <title lang="en">Olivia Jones</title>
    <desc lang="en">It may be late in the day but Olivia Jones still has a stack full of the best songs around to soundtrack your night.</desc>
    <icon src="https://images.metadata.sky.com/pd-image/e776e89e-47b9-4529-8eec-09589a7bb782/cover"/>
  </programme>
</tv>

I want to print the channel, start time, and title of matching programmes.

I came up with this:

tv / programme / title [ contains(text(), "Olivia") ] / parent::*/concat(@channel, "_", @start, "_", title)

which works at https://www.freeformatter.com/xpath-tester.html

However, it doesn't work with xqor with xmlstarlet.

Can this be done in the shell? What are the other options?


Solution

  • xmlstarlet select --template \
      --match "//tv/programme[title[contains(text(),'Olivia')]]" \
      --value-of "concat(@channel,'_',@start,'_',title)" -n file.xml
    

    Output:

    VirginRadio.uk_20250319220000 +0000_Olivia Jones