Xml

僅在一個子節點中使用 xmlstarlet 編輯 XML

  • December 6, 2017

使用命令時:

-bash-4.2$ xmlstarlet ed -u "/configurations/rules/rule/branch" -v 'DAVID' config.xml > final.xml

我看到輸出文件已更改為DAVID,但是在聲明標籤“分支”的每個地方都進行了全域更改。

但我只想在一個子節點上更改它,比如說“APP1”,我需要使用什麼命令?有沒有辦法將值’DAVID’作為參數?

 <configurations>
   <smtpHost>smtp3.gmail.com</smtpHost>
   <smtpPort>25</smtpPort>
   <emailFrom>GitPushNotifier@hpe.com</emailFrom>
   <emailSubject>Push notification</emailSubject>
   <!-- Stash general URL-->
   <gitViewerURL>http://mydtbld0005.gmail.com:7990/projects/</gitViewerURL>

   <!-- repositories list and commit URL path per repo -->

   <repositoryViewerPath name="hookTester" path="DevOps/repos/hooktester/commits/"/>

   <separator>#@#</separator>
   <catExe>cat</catExe>
   <catExeWindows>type</catExeWindows>
   <gitExe>git</gitExe>
   <gitExeWindows>C:\\Program Files (x86)\\Git\\cmd\\git.exe</gitExeWindows>
   <gitFolder>/gitdata/alm_mng.git</gitFolder>
   <gitFolderWindows>c:\gitdata\alm_mng.git</gitFolderWindows>
     <rules>
           <rule>
               <name>APP1</name>
               <repo>hookTester</repo>
               <branch>refs/heads/master</branch>
               <emailTo>test@gmail.com</emailTo>
               <path>F1/ido.xml </path>
           </rule>
           <rule>
               <name>APP2</name>
               <repo>hookTester</repo>
               <branch>refs/heads/master</branch>
               <emailTo>test@gmail.com</emailTo>
               <path>F2/ido.xml </path>
           </rule>
      </rules>
 </configurations> 

**xmlstarlet**解決方案:

new_branch="DAVID"
xmlstarlet ed -u "/configurations/rules/rule[name='APP1']/branch" -v "$new_branch" config.xml > final.xml

關鍵<rule>節點應如下所示:

<rule>
     <name>APP1</name>
     <repo>hookTester</repo>
     <branch>DAVID</branch>
     <emailTo>test@gmail.com</emailTo>
     <path>F1/ido.xml </path>
   </rule>

您還可以通過應用全域選項-L來修改初始文件:

xmlstarlet ed -L -u ... config.xml

引用自:https://unix.stackexchange.com/questions/409229