Here I'm selecting the node:
$ xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value conf/nutch-default.xml
<value/>
This doesn't edit it:
$ xmlstarlet edit "/configuration/property[name='http.agent.name']"/value -v 'test' conf/nutch-default.xml
I/O warning : failed to load external entity "/configuration/property[name='http.agent.name']/value"
What would be an xmlstartlet command that does change the change? AFAIK -x is not supported in xmlstartlet yet.
I'm working on conf/nutch-default.xml
$ xmlstarlet ed --help
XMLStarlet Toolkit: Edit XML document(s)
Usage: xml ed <global-options> {<action>} [ <xml-file-or-uri> ... ]
where
<global-options> - global options for editing
<xml-file-or-uri> - input XML document file name/uri (stdin otherwise)
<global-options> are:
-P (or --pf) - preserve original formatting
-S (or --ps) - preserve non-significant spaces
-O (or --omit-decl) - omit XML declaration (<?xml ...?>)
-N <name>=<value> - predefine namespaces (name without 'xmlns:')
ex: xsql=urn:oracle-xsql
Multiple -N options are allowed.
-N options must be last global options.
--help or -h - display help
where <action>
-d or --delete <xpath>
-i or --insert <xpath> -t (--type) elem|text|attr -n <name> -v (--value) <value>
-a or --append <xpath> -t (--type) elem|text|attr -n <name> -v (--value) <value>
-s or --subnode <xpath> -t (--type) elem|text|attr -n <name> -v (--value) <value>
-m or --move <xpath1> <xpath2>
-r or --rename <xpath1> -v <new-name>
-u or --update <xpath> -v (--value) <value>
-x (--expr) <xpath> (-x is not implemented yet)
XMLStarlet is a command line toolkit to query/edit/check/transform
XML documents (for more information see http://xmlstar.sourceforge.net/)
$ xmlstarlet --version
1.0.1
My version of
xmlstarlet
needs anaction
option to theedit
command. If you want to update the node with a new value you have to specify-u
, e.g.:You may read the entire contents of nutch-default.xml to a variable, edit the contents of that variable with xmlstarlet and then write the result back to nutch-default.xml again.
Another way would be to use open file handles as described in Redirect output from sed 's/c/d/' myFile to myFile .
The documentation is very very poor. I stumbled across Stackoverflow for more than a day and after reading through many answers on stack overflow I finally derived the solution for "edit file inplace option" for the value of an element with namespaces defined. Given an XML as below:
The xmlstarlet command to modify /config/cassandra/clusterName element value would be:
Remember ed & -L option must precede -N (namespace) option. Hope this helps somebody looking for edit file inplace option with namespace issues.