Search code examples
xpathxmlstarlet

Remove an attribute with XMLStarlet


I have an XML document and I'd like to use XMLStarlet to remove all attributes, along with their values, of a certain type from all elements (or all elements of a given type) without disturbing its other attributes.

For example, I want to remove all bad attributes from any a element:

<root>
  <a href="." bad="yes"/>
  <a bad=""/>
</root>

becomes

<root>
  <a href="."/>
  <a/>
</root>

Solution

  • XMLStarlet's ed subcommand has a --delete or -d option.

    Remove bad attribute from a elements:

    xmlstarlet ed -d '//a/@bad' input.xml
    

    Remove all bad attributes:

    xmlstarlet ed -d '//@bad' input.xml