2017-05-14 90 views
1

對於下面的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這樣的工具,但不知道這是否安裝在我們集羣的所有節點中。

任何幫助將不勝感激。 在此先感謝!

+1

「*可以使用這個SED來完成。*「請參閱:http://stackoverflow.com/a/1732454/3016153(同樣適用於XML) –

+0

您是否可以訪問'xmlstarlet'? – Cyrus

+0

很確定python在大多數系統上都帶有一個xml模塊,可以 – 123

回答

2

更新值::如果您可以在羣集上安裝xmlstarlet,你可以做以下

xmllint --shell file.xml << EOF 
cd /studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus 
set failed 
save 
EOF 

或無here document

echo -e "cd /studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus\nset failed\nsave" | xmllint --shell file.xml 

更新:在變量中使用bash和XML:

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")) 
+0

如果我的xml是在一個變量中,如何修改這個命令? – Neethu

+1

我已經更新了我的答案。 – Cyrus

+0

它的工作原理。謝謝@Cyrus。 – Neethu

2

在情況下,如果xmlstarlet(命令行工具來查詢/編輯/查看/變換 XML文檔)是可訪問的:

xmlstarlet ed -u "//studentAction/studentStatus[preceding-sibling::studentType[1][text() = 'Juniour'] \ 
      and ancestor::student/studentName[text() = 'CLASSA']]" -v failed students.xml 

上面將輸出與需要的更換

初始XML文檔

命令細節:

ed -u - 編輯/更新模式

//studentAction/studentStatus - XPath表達式來選擇具有studentStatus元件:

  • preceding-sibling::studentType[1][text() = 'Juniour'] - 同級元素studentType前述與值Juniour
  • ancestor::student/studentName[text() = 'CLASSA'] - 最近的元件studentName與值CLASSA
+0

不確定這個答案的重點,他們在評論中說它不是... – 123

+1

@ 123,這很傷心。在準備安裝程序時錯過了該評論 – RomanPerekhrest

+0

他們剛剛添加的評論看起來好像可能會是一個有用的答案,所以不要太難過 – 123

2

xlmlint as該名稱暗示,用於解析和驗證XML,而不是編輯它。在file.xmlxmllint

xmlstarlet ed --update "/studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus" --value "Failed" *file*