简体   繁体   English

Bash脚本,用于更新部分XML元素值

[英]Bash script for updating part of XML element value

I need a bash script for updating part of XML element value according to some other dynamic property. 我需要一个bash脚本来根据其他一些动态属性更新XML元素值的一部分。

For example, the XML file: 例如,XML文件:

<?xml version="1.0"  encoding="ISO-8859-1"?>
 <Configure class="org.eclipse.something">
   <Set name="foo">foo-val</Set>
   <Set name="bar">bar-val</Set>
   <Set name="my-elm">/dont/matter/THIS_ONE_NEED_TO_BE_UPDATED</Set>
 </Configure>

Tried to use xmlstarlet with regexing, but it's not providing the result I want. 尝试将xmlstarlet与正则表达式一起使用,但未提供我想要的结果。 I'm working on OSX 10.12. 我正在使用OSX 10.12。

Using xmlstarlet with below xpath expression worked fine for me. 在下面的xpath表达式中使用xmlstarlet对我来说很好。 The below expression does in-pace substitution ( -L flag) of the XML file 下面的表达式对XML文件进行即时替换( -L标志)

xmlstarlet edit -L -u "/Configure/Set[@name='my-elm']" -v '/dont/matter/THIS_ONE_NEED_TO_BE_UPDATED' xml-file

Drop the -L flag to check if the replacement is occurring properly, and once successful add the same. 删除-L标志以检查替换是否正确进行,并在成功添加后进行添加。

Checked on xmlstarlet (1.6.1) on OS X OS X上检查xmlstarlet (1.6.1)

Though it is strictly NOT advised to use sed for xml updates, this below logic will work for you, 虽然它是严格NOT建议使用sedxml的更新,这下面的逻辑会为你工作,

sed "s/\(<Set name=\"my-elm\".*>\)[^<>]*\(<\/Set.*\)/\1\/dont\/matter\/THIS_ONE_NEED_TO_BE_UPDATED\2/" xml-file

add the -i.bak for in-place substitution of the file. 添加-i.bak用于文件的就地替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM