XMLSlartlet Grep xml节点并替换

时间:2018-03-09 17:20:18

标签: xmlstarlet

如何查找节点块并使用xmlstarlet

替换自定义块

在我的xml示例下面:

<?xml version="1.0" encoding="UTF-8"?>
<job>
    <input>
    <par_denominator nil="true"/>
    <par_follow_source>true</par_follow_source>
    <par_numerator nil="true"/>
    <deblock_enable>Auto</deblock_enable>
    <deblock_strength>0</deblock_strength>
    <no_psi>false</no_psi>
    </input>
 <h264_settings>
    <par_denominator nil="true"/>
    <par_follow_source>true</par_follow_source>
    <par_numerator nil="true"/>
 </h264_settings>
</job>

我想替换所有包含

的块
    <par_denominator nil="true"/>
    <par_follow_source>true</par_follow_source>
    <par_numerator nil="true"/>

替换值

    <par_denominator>1</par_denominator>
    <par_follow_source>false</par_follow_source>
    <par_numerator>1</par_numerator>

1 个答案:

答案 0 :(得分:0)

xmlstarlet 解决方案:

xmlstarlet ed -u '//*["par_denominator" or "par_numerator"][@nil="true"]' -v 1 \
-u '//par_follow_source[.="true"]' -v 'false' \
-d '//*["par_denominator" or "par_numerator"]/@nil' input.xml

输出:

<?xml version="1.0" encoding="UTF-8"?>
<job>
  <input>
    <par_denominator>1</par_denominator>
    <par_follow_source>false</par_follow_source>
    <par_numerator>1</par_numerator>
    <deblock_enable>Auto</deblock_enable>
    <deblock_strength>0</deblock_strength>
    <no_psi>false</no_psi>
  </input>
  <h264_settings>
    <par_denominator>1</par_denominator>
    <par_follow_source>false</par_follow_source>
    <par_numerator>1</par_numerator>
  </h264_settings>
</job>