Search code examples
xmlshellxmlstarlet

to delete the value of the element in XML using xmlstarlet


I'm trying to delete the value of the element in the xml using xmlstarlet: I tried xmlstarlet ed -d command to delete the value but it did not work. I also tried using xmlstarlet ed -u to replace the value 123 with ""(null). This did not work either.

Can someone please help me how to get this: Input:

<?xml version="1.0"?>
<List Name="myList">
  <Dir Path="abc">123</Dir>
  <Dir Path="cde">456</Dir>
  <File Path="xyz">789</File>
  <File Path="xxx">567</File>
</List>

Output:

<List Name="myList">
  <Dir Path="abc"></Dir>
  <Dir Path="cde"></Dir>
  <File Path="xyz"></File>
  <File Path="xxx"></File>
</List>

I want to delete the values "123", "456" ...

Thanks in advance


Solution

  • To delete all text nodes, leaving elements and their attributes:

    xmlstarlet ed -d '//text()' <input.xml
    

    ...or, to replace the content of those text nodes with empty strings:

    xmlstarlet ed -u '//text()' -v '' <input.xml