这里我选择节点:
$ xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value conf/nutch-default.xml
<value/>
这不会编辑它:
$ 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"
确实会更改更改的 xmlstartlet 命令是什么?据我所知,xmlstartlet 尚不支持 -x。
我正在尝试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
您可以将nutch-default.xml 的全部内容读取到变量中,使用xmlstarlet 编辑该变量的内容,然后再次将结果写回nutch-default.xml。
另一种方法是使用打开的文件句柄,如中所述将输出从 sed 's/c/d/' myFile 重定向到 myFile .
xmlstarlet --version # 1.0.6
xmlstarlet ed --help | less -Ip 'inplace'
# 1.
# in-place version using xmlstarlet only
curl -L -s -o nutch-default.xml 'http://svn.apache.org/viewvc/nutch/branches/branch-1.3/conf/nutch-default.xml?view=co&revision=1079746&content-type=text%2Fplain'
xmlstarlet edit -L -u "/configuration/property[name='http.agent.name']"/value -v 'test' nutch-default.xml
xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value nutch-default.xml
# 2.
# variable version
curl -L -s -o nutch-default.xml 'http://svn.apache.org/viewvc/nutch/branches/branch-1.3/conf/nutch-default.xml?view=co&revision=1079746&content-type=text%2Fplain'
xmlstr="$(< nutch-default.xml)" # save file contents to variable
printf '%s\n' "$xmlstr" |
xmlstarlet edit -u "/configuration/property[name='http.agent.name']"/value -v 'test' > nutch-default.xml
xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value nutch-default.xml
# 3.
# file handle version
# cf. https://stackoverflow.com/questions/2585438/redirect-output-from-sed-s-c-d-myfile-to-myfile
curl -L -s -o nutch-default.xml 'http://svn.apache.org/viewvc/nutch/branches/branch-1.3/conf/nutch-default.xml?view=co&revision=1079746&content-type=text%2Fplain'
exec 3<nutch-default.xml
rm nutch-default.xml # prevent open file from being truncated
xmlstarlet edit -u "/configuration/property[name='http.agent.name']"/value -v 'test' <&3 >nutch-default.xml
xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value nutch-default.xml
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)