Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 767
Charles Avatar asked Jul 15 '26 11:07

Charles


1 Answers

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
like image 108
npostavs Avatar answered Jul 20 '26 11:07

npostavs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!