简体   繁体   中英

sed - Replace Child tag value in XML under specific Parent tag

Trying to change the value for the Child tag "Part" from 8005 to 8008 under the Parent tag "Class".

The Child Tag "Part" is used under multiple Parents, so need to reference the Parent to specify which Child tag to change.

I simplified the Init_48.xml file to only contain a single Parent.

If I do not reference the Parent in the sed command, then the change is successful.

Does anyone know why the change is not successful when referencing the Parent tag, and offer alternate code for a solution?

Please see the included image containing the sed command with results at the link sed with Results .

why the change is not successful?

The parent tag is separated from the child tag by a newline and spaces. The regex that doesn't work shows them adjacent with nothing between them.

offer alternate code for a solution

Use an XML-aware tool to handle XML. For example, in xsh ( XML::LibXML based tool I happen to maintain), you can achieve your goal with

open file.xml ;
for //Class/Part[.=8005] set . 8008 ;
save :b ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM