0

I need to create a bash script that manipulates the following sample xml file:

  1. check for a particular ID and remove that XML branch for that ID.
  2. the ID's are read from a text file.

    <?xml version="1.0"?>
    <cmtf xmlns="urn:RM_UPMS_CMTFEnvelopeSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <data xmlns="">
    <entitygroup entityname="people">
      <PERSON xmlns="abc">
        <ID ns="">12280</ID>
        <PIN xmlns="">erererre</PIN>
        <NAME xmlns="">ereffdef</NAME>
      </PERSON>
      <PERSON xmlns="bbc">
        <ID ns="">5567</ID>
        <PIN xmlns="">erererre</PIN>
        <NAME xmlns="">ereffdef</NAME>
      </PERSON>
      <PERSON xmlns="bbc">
        <ID ns="">3347</ID>
        <PIN xmlns="">ededed</PIN>
        <NAME xmlns="">rtreer</NAME>
      </PERSON>
      <PERSON xmlns="bbc">
        <ID ns="">3249</ID>
        <PIN xmlns="">erererre</PIN>
        <NAME xmlns="">ereffdef</NAME>
      </PERSON>
    </entitygroup>
    </data>
    </cmtf> 
    

Here, I need to remove all the <PERSON> tag for all the entries that have the ID 12280, 3249 which is being read from a text file.

1
  • xml-sed, part of xml-coreutils may be your friend to perform simple operations on XML files. Commented Aug 8, 2013 at 11:43

3 Answers 3

0

Perhaps you could use php like this: running php script (php function) in linux bash

And then you something like domdocument(http://php.net/manual/en/class.domdocument.php) to read and the right the xml.

Of course this is assumes you have php installed.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use XSLT for that. Create an xsl stylesheet which transform the input xml into the desired output. On the console you can use xsltproc (from xmllint package):

 xsltproc stylesheet.xsl input.xml

Comments

0

This reads a series of IDs to delete from a file input_file, and creates an output.xml based on input.xml with those entries deleted:

ed_commands=( )
while read -r num_to_delete; do
  ed_commands+=( -d "//PERSON[./ID=$num_to_delete]"
done <input_file

xmlstarlet ed "${ed_commands[@]}" <input.xml >output.xml

Note that it requires XMLStarlet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.