Tuesday, 15 January 2013

xpath - How to add text before values in xml? -



xpath - How to add text before values in xml? -

here test.xml file:

<?xml version="1.0"?> <path> <dir name="directory"> <file name="file1"/> <file name="file2"/> </dir> </path>

now can add together text after 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 add together text before values?

… <file name="_copy_file1"/> <file name="_copy_file2"/> …

according command line help, xmlstarlet ed, can utilize update -u alternative , include replacement xpath expression -x:

xmlstarlet ed -u <xpath> -x <xpath>

in replacement look can utilize xpath concat() function generate string replace attribute. look concat('_copy_',.) concatenate string '_copy_' before current node, attribute selecting. look should be:

xmlstarlet ed -u "//file[@name]/@name" -x "concat('_copy_',.)" test.xml

xml xpath xmlstarlet

No comments:

Post a Comment