【发布时间】:2017-10-13 12:36:24
【问题描述】:
对于下面的 xml,我需要将 <studentStatus> 替换为 <studentName>CLASSA</studentName> 为 <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>
到目前为止,我得到了什么,
xmllint -xpath "/studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType="Juniour"]/studentStatus" myxml.xml
现在我的学生状态为 Completed ,现在该值应更改为 Failed 。仅适用于<studentType>Juniour</studentType>。
我应该如何编辑 xml 以将其作为 ,
<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 这样的工具,但不确定它是否安装在我们集群的所有节点中。
任何帮助将不胜感激。 提前致谢!
【问题讨论】:
-
"这可以使用 sed 来完成吗。" 请参阅:stackoverflow.com/a/1732454/3016153(同样适用于 XML)。
-
你可以访问
xmlstarlet吗? -
相当肯定python在大多数系统上都带有一个xml模块,可以试试。
-
您的文件不是有效的 XML。至少缺少一个
</student>。 -
@Cyrus 已编辑。我检查了 xmlstarlet。
xmlstarlet-bash: xmlstarlet: command not found
标签: bash shell xml-parsing xmlstarlet xmllint