仅在一个子节点中使用 xmlstarlet 编辑 XML

仅在一个子节点中使用 xmlstarlet 编辑 XML

使用命令时:

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

我看到输出文件已更改为大卫,但是在声明“分支”标签的每个地方都进行了全局更改。

但我只想在一个子节点上更改它,比如说“APP1”,我需要使用什么命令?有没有办法将值“DAVID”作为参数?

  <configurations>
    <smtpHost>smtp3.gmail.com</smtpHost>
    <smtpPort>25</smtpPort>
    <emailFrom>[email protected]</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>[email protected]</emailTo>
                <path>F1/ido.xml </path>
            </rule>
            <rule>
                <name>APP2</name>
                <repo>hookTester</repo>
                <branch>refs/heads/master</branch>
                <emailTo>[email protected]</emailTo>
                <path>F2/ido.xml </path>
            </rule>
       </rules>
  </configurations> 

答案1

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>[email protected]</emailTo>
      <path>F1/ido.xml </path>
    </rule>

您还可以修改初始文件到位通过应用全局-L选项:

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

相关内容