使用xmlstarlet,如何更改元素的值

时间:2016-02-25 19:19:00

标签: bash xmlstarlet

使用xmlstarlet如何替换ThreadGroup.num_threads的所有实例的值?

 <stringProp name="ThreadGroup.num_threads">FOOO BARR</stringProp>

<stringProp name="ThreadGroup.num_threads">99999</stringProp>

这是XML片段

  <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Blogs" enabled="true">
    <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
    <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" enabled="true">
      <boolProp name="LoopController.continue_forever">false</boolProp>
      <stringProp name="LoopController.loops">778</stringProp>
    </elementProp>
    <stringProp name="ThreadGroup.num_threads">FOOO BARR</stringProp>
    <stringProp name="ThreadGroup.ramp_time">66</stringProp>
    <longProp name="ThreadGroup.start_time">44</longProp>
    <longProp name="ThreadGroup.end_time">55</longProp>
    <boolProp name="ThreadGroup.scheduler">false</boolProp>
    <stringProp name="ThreadGroup.duration">77</stringProp>
    <stringProp name="ThreadGroup.delay">0</stringProp>
  </ThreadGroup>

1 个答案:

答案 0 :(得分:2)

您可以使用xmlstarlet ed -u进行此操作。指定XPath表达式,然后使用-v设置新值:

xmlstarlet ed -u '/ThreadGroup/stringProp[@name="ThreadGroup.num_threads"]' \
                -v 99999 file.xml

(根据XML文件的其余部分,您可能必须修改XPath表达式以使其更具体,并考虑更多嵌套)

这是一个完整的例子:

$ cat file.xml
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Blogs" enabled="true">
  <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
  <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" enabled="true">
    <boolProp name="LoopController.continue_forever">false</boolProp>
    <stringProp name="LoopController.loops">778</stringProp>
  </elementProp>
  <stringProp name="ThreadGroup.num_threads">99999</stringProp>
  <stringProp name="ThreadGroup.ramp_time">66</stringProp>
  <longProp name="ThreadGroup.start_time">44</longProp>
  <longProp name="ThreadGroup.end_time">55</longProp>
  <boolProp name="ThreadGroup.scheduler">false</boolProp>
  <stringProp name="ThreadGroup.duration">77</stringProp>
  <stringProp name="ThreadGroup.delay">0</stringProp>
</ThreadGroup>

$ xmlstarlet ed -u '/ThreadGroup/stringProp[@name="ThreadGroup.num_threads"]' \
                -v 99999 file.xml
<?xml version="1.0"?>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Blogs" enabled="true">
  <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
  <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" enabled="true">
    <boolProp name="LoopController.continue_forever">false</boolProp>
    <stringProp name="LoopController.loops">778</stringProp>
  </elementProp>
  <stringProp name="ThreadGroup.num_threads">99999</stringProp>
  <stringProp name="ThreadGroup.ramp_time">66</stringProp>
  <longProp name="ThreadGroup.start_time">44</longProp>
  <longProp name="ThreadGroup.end_time">55</longProp>
  <boolProp name="ThreadGroup.scheduler">false</boolProp>
  <stringProp name="ThreadGroup.duration">77</stringProp>
  <stringProp name="ThreadGroup.delay">0</stringProp>
</ThreadGroup>