對於下面的xml,我需要將<studentStatus>
替換爲<studentName>CLASSA</studentName>
至<studentStatus>failed</studentStatus>
。解析xml並替換特定標籤shell腳本
<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
現在我得到了學生的狀態爲已完成,現在這個值應該是失敗的改變。僅適用於<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>
</studentFile>
可以這樣使用SED來完成。我知道有像xsltproc這樣的工具,但不知道這是否安裝在我們集羣的所有節點中。
任何幫助將不勝感激。 在此先感謝!
「*可以使用這個SED來完成。*「請參閱:http://stackoverflow.com/a/1732454/3016153(同樣適用於XML) –
您是否可以訪問'xmlstarlet'? – Cyrus
很確定python在大多數系統上都帶有一個xml模塊,可以 – 123