Here is my test.xml file:
<?xml version="1.0"?>
<path>
<dir name="directory">
<file name="file1"/>
<file name="file2"/>
</dir>
</path>
Now I can add some text after my values:
xmlstarlet ed -s "//file[@name]/@name" -t text -n "@name" -v "_copy_" test.xml
Result:
…
<file name="file1_copy_"/>
<file name="file2_copy_"/>
…
How can I add a text before values?
…
<file name="_copy_file1"/>
<file name="_copy_file2"/>
…
According to the command line help, with xmlstarlet ed, you can use the update -u option and include a replacement XPath expression -x:
xmlstarlet ed -u <xpath> -x <xpath>
In the replacement expression you can use an XPath concat() function to generate the string which will replace the attribute. The expression concat('_copy_',.) will concatenate the string '_copy_' before the current node, which is the attribute you are selecting. Your expression should be:
xmlstarlet ed -u "//file[@name]/@name" -x "concat('_copy_',.)" test.xml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With