如何编写一个bash脚本,它可以解析XML文件作为输入并从文件中删除某些元素

时间:2021-04-27 15:43:16

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

我需要创建一个操作以下示例xml文件的bash脚本:

  1. check for a particular ID and remove that XML branch for that ID.
  2. 检查特定ID并删除该ID的XML分支。

  3. the ID's are read from a text file.

    ID是从文本文件中读取的。

    <?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.

在这里,我需要删除所有从文本文件中读取ID为12280,3249的条目的 标记。

3 个解决方案

#1


0  

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

也许你可以使用这样的php:在linux bash中运行php脚本(php函数)

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

然后你就像domdocument(http://php.net/manual/en/class.domdocument.php)来读取和正确的xml。

Of course this is assumes you have php installed.

当然这是假设你安装了php。

#2


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):

您可以使用XSLT。创建一个xsl样式表,将输入xml转换为所需的输出。在控制台上,您可以使用xsltproc(来自xmllint包):

 xsltproc stylesheet.xsl input.xml

#3


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:

这将读取要从文件input_file中删除的一系列ID,并基于input.xml创建一个output.xml,并删除这些条目:

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.

请注意,它需要XMLStarlet。

#1


0  

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

也许你可以使用这样的php:在linux bash中运行php脚本(php函数)

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

然后你就像domdocument(http://php.net/manual/en/class.domdocument.php)来读取和正确的xml。

Of course this is assumes you have php installed.

当然这是假设你安装了php。

#2


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):

您可以使用XSLT。创建一个xsl样式表,将输入xml转换为所需的输出。在控制台上,您可以使用xsltproc(来自xmllint包):

 xsltproc stylesheet.xsl input.xml

#3


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:

这将读取要从文件input_file中删除的一系列ID,并基于input.xml创建一个output.xml,并删除这些条目:

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.

请注意,它需要XMLStarlet。