0

I need to copy an xml tag and its value to another xml file under same root tag. Create the root tag if it doesn't exist.

Here is an input output example.

INPUT: string.xml
<resource>
    <string name="app_name">MyExampleApp</string>
    <string name="profile">My Profile</string>
    <string name="messages">My Messages</string>
</resource>

$ command "profile" string.xml string1.xml

OUTPUT: string1.xml
<resource>
    <string name="profile">My Profile</string>
</resource>

I was looking for that "Command" and options. Not necessarily in that order. Please help me out here. Any Linux/Unix/Mac command will do

5

Using XMLStarlet, you could delete all the string nodes under the root node whose name attribute is not profile:

$ xml ed -d '/resource/string[@name != "profile"]' string.xml
<?xml version="1.0"?>
<resource>
  <string name="profile">My Profile</string>
</resource>

Add -O after ed if you don't want the XML declaration line.

  • I think -t is not needed here (it's an option for sel, not ed). – Satō Katsura Aug 10 '16 at 7:42
  • @SatoKatsura You're right, xml silently ignores it. I'll amend the answer. – Kusalananda Aug 10 '16 at 7:46
  • xml ed -t segfaults here... – Satō Katsura Aug 10 '16 at 7:47
  • @SatoKatsura Oopsie. What's the system and where did you get XMLStarlet from? I'm on OS X with XMLStarlet from NetBSD's pkgsrc. – Kusalananda Aug 10 '16 at 7:48
  • OpenBSD (from ports) and Linux (compiled from sources). – Satō Katsura Aug 10 '16 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.