Through another stackoverflow answer (Extracting values from XML file...,) I came a cross the command line tool xmlstarlet
(Download at xmlstar.sourceforge.net, tutorial at www.geekfarm.org) which is able to do the job:
Say, the XML file is saved as preset preset-test.xml
. Then use in your shell
xml el preset-test.xml
to get an overview over the XML structure. Then type
xml sel -t -c "(//string[preceding-sibling::key[1] = 'inputCompositeImagePath'])/text()" preset-test.xml
# (blank line for firefox rendering of stackoverflow code class)
to extract the wanted information. It prints as expected:
/Users/me/imageA.psd
/Users/me/imageB.psd
Note: The magic is obviously in the XPath expression (//string[preceding-sibling::key[1] = 'inputCompositeImagePath'])/text()
. It works as follows:
//string[...]
selects all tags with the namestring
, butpreceding-sibling::key[1] = 'inputCompositeImagePath'
restricts this selection to tags where the first preceding sibling has the value'inputCompositeImagePath'
./text()
than selects from this selection the textual content.
The xmlstarlet
tool is maybe not as easy to use as I hoped for, but due to its full XPath functionality pretty powerful.