Search code examples
xmlbashxmlstarlet

How to change the format of a tag in xml file?


In an xml file, how to change all occurrence of annotation tag which is in format:

<manifest>
<project name="something" path="something" revision="some sha"><annotation version="1.2.3" /></project>
</manifest>

to:

<manifest>
<project name="something" path="something" revision="some sha"><annotation name="version" value="1.2.3" /></project>
</manifest>

I couldn't figure out options to do this using xmlstarlet


Solution

  • xmlstarlet edit --omit-decl \
      --insert '//manifest/project/annotation' --type 'attr' -n 'name' --value version \
      --rename '//manifest/project/annotation/@version' -v 'value' file.xml
    

    Output:

    <manifest>
      <project name="something" path="something" revision="some sha">
        <annotation value="1.2.3" name="version"/>
      </project>
    </manifest>