LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-17-2012, 03:03 PM   #1
userofunix
LQ Newbie
 
Registered: Sep 2012
Posts: 1

Rep: Reputation: Disabled
How to search for a corresponding tag in xml file and replace the corresponding value


Hello Everyone,

I've to modify hundreds of xml files by looking for a one or more specific tags and then change the corresponding value for the ones matching. Is it possible to do using a quick shell script.

Let's say, I've an xml file like below and the matching tags file contain 'abcd' and 'efgh', then I want to toggle the 'ignore' arg for those two only to true.

i/p

<?xml version="1.0" encoding="UTF-8"?>
<test-case id="">
<import name="abcd" id="id1" module-dir="loc1">
<arg name="arg1" value="${val1}"/>
<arg name="arg2" value="${val2}"/>
<arg name="ignore" value="false"/>
</import>
<import name="xyz" id="id2" module-dir="loc1">
<arg name="arg1" value="${val1}"/>
<arg name="arg2" value="${val2}"/>
<arg name="ignore" value="false"/>
</import>
<import name="efgh" id="id3" module-dir="loc2">
<arg name="arg1" value="${val1}"/>
<arg name="arg2" value="${val2}"/>
<arg name="ignore" value="false"/>
</import>
...
</test-case>

after running the script, I want the file to look like:

<?xml version="1.0" encoding="UTF-8"?>
<test-case id="">
<import name="abcd" id="id1" module-dir="loc1">
<arg name="arg1" value="${val1}"/>
<arg name="arg2" value="${val2}"/>
<arg name="ignore" value="true"/>
</import>
<import name="xyz" id="id2" module-dir="loc1">
<arg name="arg1" value="${val1}"/>
<arg name="arg2" value="${val2}"/>
<arg name="ignore" value="false"/>
</import>
<import name="efgh" id="id3" module-dir="loc2">
<arg name="arg1" value="${val1}"/>
<arg name="arg2" value="${val2}"/>
<arg name="ignore" value="true"/>
</import>
...
</test-case>
 
Old 09-17-2012, 03:42 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Using XMLStarlet:

Code:
find path/to/xml/files/ -name '*.xml' -print0 | \
 xargs -0 xmlstarlet ed -L -P \
  -u '/test-case/import[@name = "abcd" or @name = "efgh"]/arg[@name = "ignore"]/@value' \
  -x 'not(. = "true")'
Quote:
then I want to toggle the 'ignore' arg for those two only to true.
The above code toggles, if you meant just set to true always then the last line should be
Code:
-v true
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add xml-stylesheet tag in a XML File using libxml2 ? peacemission Programming 6 05-26-2012 03:20 AM
Adding new tag to an xml file using sed nano2 Linux - General 6 11-23-2011 06:47 AM
how to find the end tag in xml file. mariakumar Linux - General 1 12-21-2010 11:12 AM
Perl Search and Replace XML tags conditionally rammyp_1979 Programming 15 10-22-2010 10:11 AM
How To get the data from a tag in XML File kingmaker2003 Programming 7 12-05-2008 12:12 AM


All times are GMT -5. The time now is 02:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration