简体   繁体   English

xmlstarlet-删除xml元素-Shell脚本

[英]xmlstarlet - delete a xml element - shell scripting

My XML : 我的XML:

<?xml version='1.0' encoding='UTF-8'?>
<mm2-moduleset plugin="mm2-plugin@2.27.0-SNAPSHOT">
..
    <buildType class="hudson.mm.MMModuleSet$Component">
       <viewableName>Component</viewableName>
    </buildType>
    <selectedCodeComponents>
       <string>admin-content</string>
       <string>admin</string>
    </selectedCodeComponents>
    <incrementalBuild>false</incrementalBuild>
    ...
    ...
</mm2-moduleset>
</xml>

Basically, im trying to get the job details from the jenkins-cli plugin, and update the configuration of the xml file(the configuration of the jenkins job) and then update the jenkins job using shell-script. 基本上,我试图从jenkins-cli插件中获取作业详细信息,并更新xml文件的配置(jenkins作业的配置),然后使用shell脚本更新jenkins作业。

For that, I need to replace "string" attribute values of the "selectedCodeComponents" attribute according to the user input using shell script. 为此,我需要根据用户使用shell脚本输入的内容来替换“ selectedCodeComponents”属性的“ string”属性值。 if the input is check, check-content, bank, then 如果输入是支票,支票内容,银行,则

    <selectedCodeComponents>
       <string>check-content</string>
       <string>check</string>
       <string>bank</string>     
    </selectedCodeComponents>

My code: 我的代码:

${JAVA_HOME}/bin/java -Djavax.net.ssl.trustStore=${JENKINS_HOST}.keystore -Djavax.net.ssl.trustStorePassword=112012 -jar ./jenkins-cli.jar -s https://$USERNAME:$API_TOKEN\@$JENKINS_HOST:$JENKINS_PORT\/jenkins get-job job_1 > job1.xml

(Jenkins-cli - get-job returns configuration in xml file) (Jenkins-cli-get-job返回xml文件中的配置)

I thought of first deleting the string elements in selectedcodecomponents and append with what ever user gives. 我想到了先删除selectedcodecomponents中的字符串元素,然后附加用户提供的内容。 Like this, 像这样,

xmlstarlet ed -d "mm2-moduleset/selectedCodeComponents/string" abc.xml

// some logic
xmlstarlet ed -a "mm2-moduleset/selectedCodeComponents/string" --type elem -n string -v "something" abc.xml

But after executing the first command (xmlstarlet ed -d), it deletes the seletedcodecomponents as well. 但是在执行第一个命令(xmlstarlet ed -d)之后,它也会删除seletedcodecomponents。 But i wanted to delete only string elements inside it. 但是我只想删除其中的字符串元素。

<?xml version='1.0' encoding='UTF-8'?>
 <mm2-moduleset plugin="mm2-plugin@2.27.0-SNAPSHOT">
..
    <buildType class="hudson.mm.MMModuleSet$Component">
       <viewableName>Component</viewableName>
    </buildType>
    </selectedCodeComponents>
    <incrementalBuild>false</incrementalBuild>
    ...
    ...
 </mm2-moduleset>
</xml>,

append command also appending twice.(xmlstarlet ed -a ) For ex, append命令也会附加两次。(xmlstarlet ed -a)例如,

xmlstarlet ed -a "mm2-moduleset/selectedCodeComponents/string" --type elem -n string -v "check" abc.xml


<?xml version='1.0' encoding='UTF-8'?>
<mm2-moduleset plugin="mm2-plugin@2.27.0-SNAPSHOT">
..
    <buildType class="hudson.mm.MMModuleSet$Component">
       <viewableName>Component</viewableName>
    </buildType>
    <selectedCodeComponents>
       <string>admin-content</string>
       <string>check</string>
       <string>admin</string>
       <string>check</string>

    </selectedCodeComponents>
    <incrementalBuild>false</incrementalBuild>
    ...
    ...
</mm2-moduleset>
</xml>

could you please tell me how to delete only string elements inside selectedcodecomponents element and why append command appends twice 您能否告诉我如何只删除selectedcodecomponents元素内的字符串元素,以及为什么append命令追加两次

I'm new to shell script hence your suggestions would help me 我是Shell脚本的新手,因此您的建议将对我有所帮助

Fixing up your sample XML file a bit, to give it a root tag: 稍微修复示例XML文件,为其提供一个根标记:

$ cat file.xml
<?xml version='1.0' encoding='UTF-8'?>
<root>
    <buildType class="hudson.mm.MMModuleSet$Component">
       <viewableName>Component</viewableName>
    </buildType>
    <selectedCodeComponents>
       <string>admin-content</string>
       <string>admin</string>
    </selectedCodeComponents>
    <incrementalBuild>false</incrementalBuild>
</root>

You delete the string nodes like this 您像这样删除string节点

$ xmlstarlet ed -d '//selectedCodeComponents/string' file.xml 
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <buildType class="hudson.mm.MMModuleSet$Component">
    <viewableName>Component</viewableName>
  </buildType>
  <selectedCodeComponents/>
  <incrementalBuild>false</incrementalBuild>
</root>

And then add new string subnodes like this 然后像这样添加新的字符串子节点

$ xmlstarlet ed -d '//selectedCodeComponents/string' file.xml |
xmlstarlet ed -s '//selectedCodeComponents' -t elem -n string -v something1 |
xmlstarlet ed -s '//selectedCodeComponents' -t elem -n string -v something2 |
xmlstarlet ed -s '//selectedCodeComponents' -t elem -n string -v something3
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <buildType class="hudson.mm.MMModuleSet$Component">
    <viewableName>Component</viewableName>
  </buildType>
  <selectedCodeComponents>
    <string>something1</string>
    <string>something2</string>
    <string>something3</string>
  </selectedCodeComponents>
  <incrementalBuild>false</incrementalBuild>
</root>

1) if you need to delete whole string elements, use below code 1)如果您需要删除整个字符串元素,请使用以下代码

xmlstarlet ed -P -S -d "/mm2-moduleset/.../selectedCodeComponents/string" config.xml >temp.xml
mv temp.xml config.xml

2) if you have to delete a particular string element, use below code 2)如果您必须删除特定的字符串元素,请使用以下代码

read -p "read the string that need to be deleted" deleteString

xmlstarlet ed -P -S -d "/mm2-moduleset/.../selectedCodeComponents/string[text()='$deleteString']" config.xml >temp.xml
mv temp.xml config.xml

3) if you have to replace the strings based on user input, you can try the below code 3)如果必须根据用户输入替换字符串,则可以尝试以下代码

read - "read the string that need to be modify" modifyString1 modifyString2 modifyString3

xmlstarlet ed -P -S -s "/mm2-moduleset/.../selectedCodeComponents" -t elem -n string -v $modifyString1 -s "/mm2-moduleset/.../selectedCodeComponents" -t elem -n string -v $modifyString2 -s "/mm2-moduleset/.../selectedCodeComponents" -t elem -n string -v $modifyString3 config.xml >temp.xml
mv temp.xml config.xml

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

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