I was looking at a possible XML-based solution to another question. I'm aware of its accepted answer, which offers sed
and python
solutions.
The given XML fragment is:
<Names>
<Name>Luigi</Name>
<Name>Mario</Name>
<Name>Peach</Name>
</Names>
and the requirement is to change the <Name/>
element names to a sequence <Name1/>
, <Name2/>
, <Name3/>
, etc.
Using xmlstarlet
I can rename the <Name/>
element values to an fixed value with something like this
xmlstarlet ed -u '//Name' -v 'Another' names.xml
<Names>
<Another>Luigi</Another>
<Another>Mario</Another>
<Another>Peach</Another>
</Names>
And I can even change the element values to an expression with -x
(--expr
).
But for editing element names themselves there is no -x
(--expr
) option as an alternative to the -v
(--value
) option, so I cannot use something like this:
xmlstarlet ed -u '//Name' -x 'concat(., position())' names.xml
Using an XML-aware tool such as xmlstarlet
, is it possible to transform the input XML to differentiate the <Name/>
elements like this, and if so, how?
<Names>
<Name1>Luigi</Name1>
<Name2>Mario</Name2>
<Name3>Peach</Name3>
</Names>
sed
!