Searching for XML tag by value between them and inserting a new tag in shell script

回眸只為那壹抹淺笑 提交于 2019-12-13 04:07:17

问题


Say I have this test.xml file which has these contents

<d>
  <p>
    <n>hi</n>
    <r>
      <s>1.0</s>
    </r>
  </p>

  <p>
    <n>hello</n>
    <r>
      <s>1.0</s>
    </r>
  </p>
</d>

I want to add a new <s>2.0</s> for "hello" object as shown below.

<d>
  <p>
    <n>hi</n>
    <r>
      <s>1.0</s>
    </r>
  </p>

  <p>
    <n>hello</n>
    <r>
      <s>1.0</s>
      <s>2.0</s>
    </r>
  </p>
</d>

I have to do this using shell script. There is a way of searching through the XML DOM and adding tags using xmlstarlet given here http://www.technomancy.org/xml/add-a-subnode-command-line-xmlstarlet/ . But this only describes adding new tag to certain nodes based on attribute value. I do not have any attributes. How can I do it ? Is there any way of doing it using grep ?


回答1:


xmlstarlet ed -a '//p[n="hello"]/r/s' -t elem -n s -v 2.0 input.xml

Explanation:

  • ed ==> edit
  • -a ==> append
  • -t ==> type
  • -n ==> name
  • -v ==> value


来源:https://stackoverflow.com/questions/9726143/searching-for-xml-tag-by-value-between-them-and-inserting-a-new-tag-in-shell-scr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!