2017-05-14 5 views
1

以下のxmlについては、<studentName>CLASSA</studentName><studentStatus>failed</studentStatus>の代わりに<studentStatus>を置き換える必要があります。xmlを解析して特定のタグを置き換えるシェルスクリプト

<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>のみ はどうすれば

<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を使用して行うことができ、としてそれを得るために順序どおりXMLを編集する必要があります。 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

それは動作します。ありがとう@サイラス。 – Neethu

2

アクセス可能です(/編集を照会するためのコマンドラインツールキット/ XML文書を変換/チェック)xmlstarlet場合:

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'] - 値Juniour
  • ancestor::student/studentName[text() = 'CLASSA']と兄弟要素studentType先行 - 値に最も近い要素studentNameCLASSA
+0

コメントには答えがありません。 – 123

+1

@ 123、それはかなり悲しいです。アンサーを準備中にそのコメントを逃した – RomanPerekhrest

+0

彼らはちょうどそれが有用な答えになるように思われるので、あまりにも悲しい – 123

2

xlmlint、名前が意味するのは、それを編集するのではなく、XMLの解析と検証です。 file.xmlxmllint

xmlstarlet ed --update "/studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus" --value "Failed" *file* 
関連する問題