xmlbashxmlstarlet

xmlstarlet insert fails without error message


I have an XML file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<ChannelList>
  <channel>
    <tvg-logo>https://tvpmlogopus.samsungcloud.tv/platform/image/sourcelogo/vc/70/01/68/USAJ2200014K9_20201208T081550.png</tvg-logo>
    <group-title>Samsung TV Plus</group-title>
    <title>US | ION Plus</title>
    <url>https://live-news-manifest.tubi.video/live-news-manifest/csm/extlive/tubiprd01,ION-Plus.m3u8</url>
  </channel>
  <channel>
    <tvg-logo>https://od.lk/s/MF8yMjY0NDYwOTdf/Stirr_Blue_510x510.png</tvg-logo>
    <group-title>"STIRR TV",STIRR</group-title>
    <title>Electric Now</title>
    <url>https://dai.google.com/linear/hls/event/KsvJAc81Qoewj6opYso6Fw/master.m3u8</url>
  </channel>

</ChannelList>

I want to add some elements through a bash script using xmlstarlet. Based on what I've read, I've got this command:

xmlstarlet ed -L -i "/ChannelList/channel/title/" -v "NewTitle" > channels.xml

When I run it, there is no error message, and it deletes the contents of the xml file. I thought that -L should edit the file in place, but it deletes the contents if I remove it or leave it in. -i should insert, but it looks like I'm missing something.

When I run the command I get the --help contents on the screen and the xml file is empty. The file still exists. Not that it should matter, but it's running inside a docker container. XML file is owned by root, and has 777 permissions on it. (This will be resolved once I can get it to work)


Solution

  • Assuming channels.xml is the name of your input XML file, you can do in-place (-L) insertion by :

    xmlstarlet ed -L\
        -s /ChannelList -t elem -n channel -v ""\
        -s "/ChannelList/channel[last()]" -t elem -n "tvg-logo" -v "NewLogo"\
        -s "/ChannelList/channel[last()]" -t elem -n "title"    -v "NewTitle"\
        channels.xml