解析xml并替换特定标签的shell脚本 [英] Parsing xml to and replacing specific tags shell script
问题描述
对于下面的xml,我需要将<studentName>CLASSA</studentName>
的<studentStatus>
替换为<studentStatus>failed</studentStatus>
.
For the below xml ,I need to replace <studentStatus>
for <studentName>CLASSA</studentName>
to <studentStatus>failed</studentStatus>
.
<studentFile>
<student>
<studentName>CLASSA</studentName>
<studentStatus>Success</studentStatus>
<studentActions>
<studentAction>
<studentType>Juniour</studentType>
<studentStatus>Completed</studentStatus>
<studentMsg/>
</studentAction>
<studentAction>
<studentType>HighSchool</studentType>
<studentStatus>Completed</studentStatus>
<studentMsg/>
</studentAction>
</studentActions>
</student>
<student>
<studentName>CLASSB</studentName>
<studentStatus>Success</studentStatus>
<studentActions>
<studentAction>
<studentType>Senior</studentType>
<studentStatus>Completed</studentStatus>
</studentAction>
<studentAction>
<studentType>Middle</studentType>
<studentStatus>Completed</studentStatus>
</studentAction>
</studentActions>
</student>
</studentFile>
我到目前为止所得到的,
What I got so far,
xmllint -xpath "/studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType="Juniour"]/studentStatus" myxml.xml
现在我的学生状态为已完成",现在该值应更改为失败".仅用于<studentType>Juniour</studentType>
.
我应该如何编辑xml以使其为
now i got the status of the student as Completed , now this value should be changed to Failed . Only for <studentType>Juniour</studentType>
.
How should I edit the xml inorder to get it as ,
<studentFile>
<student>
<studentName>CLASSA</studentName>
<studentStatus>Success</studentStatus>
<studentActions>
<studentAction>
<studentType>Juniour</studentType>
<studentStatus>Failed</studentStatus>
<studentMsg/>
</studentAction>
<studentAction>
<studentType>HighSchool</studentType>
<studentStatus>Completed</studentStatus>
<studentMsg/>
</studentAction>
</studentActions>
</student>
<student>
<studentName>CLASSB</studentName>
<studentStatus>Success</studentStatus>
<studentActions>
<studentAction>
<studentType>Senior</studentType>
<studentStatus>Completed</studentStatus>
</studentAction>
<studentAction>
<studentType>Middle</studentType>
<studentStatus>Completed</studentStatus>
</studentAction>
</studentActions>
</student>
</studentFile>
可以使用sed完成此操作.我知道有xsltproc之类的工具,但不确定是否在集群的所有节点中都安装了该工具.
Can this be done using sed. I know there are tools like xsltproc but not sure if this is installed in all nodes in our cluster .
任何帮助将不胜感激. 预先感谢!
Any help will be appreciated. Thanks in advance!
推荐答案
在file.xml
中用xmllint
更新值:
xmllint --shell file.xml << EOF
cd /studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus
set failed
save
EOF
或没有此处是文档:
echo -e "cd /studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus\nset failed\nsave" | xmllint --shell file.xml
更新:在变量中包含bash和XML:
Update: With bash and XML in a variable:
xml=$(xmllint --shell <(echo "$xml") << EOF
cd /studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus
set failed
save -
EOF
)
或没有此处的文档:
xml=$(echo -e "cd /studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus\nset failed\nsave -" | xmllint --shell <(echo "$xml"))
这篇关于解析xml并替换特定标签的shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!