我使用xmlstarlet编辑一些xml配置文件。默认文件如下所示:
<configuration>
<property>
<name></name>
<value></value>
</property>
<property>
<name></name>
<value></value>
</property>
</configuration>
我正在添加更多<property>
个子节点。像这样:
sudo xmlstarlet ed -L \
-s '//configuration' -t elem -n "property" \
-s '//configuration/property[last()]' -t elem -n "name" -v "test-1" \
-s '//configuration/property[last()]' -t elem -n "value" -v "00001" \
-s '//configuration' -t elem -n "property" \
-s '//configuration/property[last()]' -t elem -n "name" -v "test-2" \
-s '//configuration/property[last()]' -t elem -n "value" -v "00002" \
/etc/path/to/file.xml
我在本地测试了这个,使用了XMLStarlet 1.6.1,它运行得非常好,而且我想要它。结果如下:
<configuration>
<property>
<name></name>
<value></value>
</property>
<property>
<name></name>
<value></value>
</property>
<property>
<name>test-1</name>
<value>00001</value>
</property>
<property>
<name>test-2</name>
<value>00002</value>
</property>
</configuration>
但是,当我把它放在一个shell脚本中,然后尝试在我的远程实例(使用XMLStarlet 1.3.1)上运行它时,它会一直打印出包的帮助文本,就像你只需输入xmlstarlet
一样终点站。我不知道是什么导致了这个
[remote-machine ~]$ xmlstarlet
XMLStarlet Toolkit: Command line utilities for XML
Usage: xmlstarlet [<options>] <command> [<cmd-options>]
where <command> is one of:
ed (or edit) - Edit/Update XML document(s)
sel (or select) - Select data or query XML document(s) (XPATH, etc)
tr (or transform) - Transform XML document(s) using XSLT
val (or validate) - Validate XML document(s) (well-formed/DTD/XSD/RelaxNG)
fo (or format) - Format XML document(s)
el (or elements) - Display element structure of XML document
c14n (or canonic) - XML canonicalization
ls (or list) - List directory as XML
esc (or escape) - Escape special XML characters
unesc (or unescape) - Unescape special XML characters
pyx (or xmln) - Convert XML into PYX format (based on ESIS - ISO 8879)
p2x (or depyx) - Convert PYX into XML
<options> are:
--version - show version
--help - show help
Wherever file name mentioned in command help it is assumed
that URL can be used instead as well.
Type: xmlstarlet <command> --help <ENTER> for command help
XMLStarlet is a command line toolkit to query/edit/check/transform
XML documents (for more information see http://xmlstar.sourceforge.net/)
xmlstarlet ed -s
在这两个版本中都有效;改变了什么,使我的脚本特定于版本?
答案 0 :(得分:1)
在XMLStarlet 1.3.1(您在远程系统上),-v
的{{1}}参数是必需的。
因此,您需要在添加xmlstarlet ed -s
元素时添加-v ''
。
property