I have the following xml file that I would like to swap the positions of the two siblings ("firstname" and "surname"):
From:
<?xml version="1.0" encoding="utf-8"?>
<article>
<name>
<firstname>Peter</firstname>
<surname>Frenchman</surname>
</name>
<name>
<firstname>Betty</firstname>
<surname>Jones</surname>
</name>
</article>
to:
<?xml version="1.0" encoding="utf-8"?>
<article>
<name>
<surname>Frenchman</surname>
<firstname>Peter</firstname>
</name>
<name>
<surname>Jones</surname>
<firstname>Betty</firstname>
</name>
</article>
This is what I did:
I created a temp element (surname1) and copy the values from "surname" to it. The xmlstarlet command did not do the work:
xmlstarlet ed -L -i "//article/name/givenname" -t elem -n "surname1" -v "$(xmlstarlet sel -t -v "//article/name/surname" swapelment.xml)" swapelment.xml
Here is how to implement it via XSLT.
It is using a so called Identity Transform pattern.
XMLStarlet natively supports XSLT transformation: XMLStarlet Overview
Usage:
xml tr <xsl-file> <xml-file-or-uri>Input XML
XSLT
Output XML