Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

bash - Using xmlstarlet, how do I change the value of an element

Using xmlstarlet how can replace the value for all instances of ThreadGroup.num_threads please?

Before

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

After

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

Here's the XML snippet

  <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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can du this with xmlstarlet ed -u. Specify an XPath expression and then set the new value with -v:

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

(Depending on the rest of your XML file, you may have to modify the XPath expression to be more specific and account for more nesting)

Here's a complete example:

$ 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...