使用 BASH 在 XML 文件中创建新元素

使用 BASH 在 XML 文件中创建新元素

到目前为止,我已经使用 sed、awk、while 循环等在 bash 中完成了一些简单的事情。

我接下来要做的事情要复杂得多,我不知道如何开始。

我有一个 XML 文件,我需要根据 XML 文件中的其他信息向其中添加元素。

我应该使用什么工具来实现这一点? while 循环数组sed

  1. 获取复合 ID 列表并将它们存储在数组中

  2. 对于每个复合 ID,搜索与复合 ID 匹配的所有单独 ID,并将每个单独 ID 的结果存储在数组中。

a. 如果任何一个单独的 ID 具有正向标签,则在 XML 文件中创建一个新的并将其设置为正向。

b. 如果所有单独的 ID 都具有负数标签,则在 XML 文件中创建一个新的并将其设置为负数。

例如,这是原始 XML 文件的示例:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<waterSample>
    <HeaderInformation>
        <ReportSerialNumber>987754</ReportSerialNumber>
    </HeaderInformation>
    <sample>
        <site>40</site>
        <sampleid>040-2016-12387</sampleid>
        <productcode>AQUA</productcode>
        <productdescription>10ozAquafina</productdescription>
        <testcode>Oxygen</testcode>
        <testdescription>Test for Oxygen</testdescription>
        <sequence>0</sequence>
        <sampledate>2016-05-27T16:03:00-05:00</sampledate>
        <prefix></prefix>
        <addendum></addendum>
        <compositeid></compositeid>
        <samplename>Water</samplename>
        <sampledescription>Tasty Water</sampledescription>
        <labsampleid>789-879717-001</labsampleid>
        <receivedby>John Doe</receivedby>
        <receivetime>2016-05-28T10:49:43-04:00</receivetime>
        <reporttime>2016-05-30T11:30:11-04:00</reporttime>
        <result>Negative</result>
        <units>10oz</units>
    </sample>
<sample>
        <site>40</site>
        <sampleid>040-2016-12388</sampleid>
        <productcode>AQUA</productcode>
        <productdescription>10ozAquafina</productdescription>
        <testcode>Oxygen</testcode>
        <testdescription>Test for Oxygen</testdescription>
        <sequence>0</sequence>
        <sampledate>2016-05-27T16:03:00-05:00</sampledate>
        <prefix></prefix>
        <addendum></addendum>
        <compositeid>78979</compositeid>
        <samplename>Water</samplename>
        <sampledescription>Tasty Water</sampledescription>
        <labsampleid>789-879717-002</labsampleid>
        <receivedby>John Doe</receivedby>
        <receivetime>2016-05-28T10:49:43-04:00</receivetime>
        <reporttime>2016-05-30T11:30:11-04:00</reporttime>
        <result>Negative</result>
        <units>10oz</units>
    </sample>
<sample>
        <site>40</site>
        <sampleid>040-2016-12389</sampleid>
        <productcode>AQUA</productcode>
        <productdescription>10ozAquafina</productdescription>
        <testcode>Oxygen</testcode>
        <testdescription>Test for Oxygen</testdescription>
        <sequence>0</sequence>
        <sampledate>2016-05-27T16:03:00-05:00</sampledate>
        <prefix></prefix>
        <addendum></addendum>
        <compositeid>78979</compositeid>
        <samplename>Water</samplename>
        <sampledescription>Tasty Water</sampledescription>
        <labsampleid>789-879717-003</labsampleid>
        <receivedby>John Doe</receivedby>
        <receivetime>2016-05-28T10:49:43-04:00</receivetime>
        <reporttime>2016-05-30T11:30:11-04:00</reporttime>
        <result>Positive</result>
        <units>10oz</units>
    </sample>
</waterSample>

代码运行后文件的样子如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<waterSample>
    <HeaderInformation>
        <ReportSerialNumber>987754</ReportSerialNumber>
    </HeaderInformation>
    <sample>
        <site>40</site>
        <sampleid>040-2016-12387</sampleid>
        <productcode>AQUA</productcode>
        <productdescription>10ozAquafina</productdescription>
        <testcode>Oxygen</testcode>
        <testdescription>Test for Oxygen</testdescription>
        <sequence>0</sequence>
        <sampledate>2016-05-27T16:03:00-05:00</sampledate>
        <prefix></prefix>
        <addendum></addendum>
        <compositeid></compositeid>
        <samplename>Water</samplename>
        <sampledescription>Tasty Water</sampledescription>
        <labsampleid>789-879717-001</labsampleid>
        <receivedby>John Doe</receivedby>
        <receivetime>2016-05-28T10:49:43-04:00</receivetime>
        <reporttime>2016-05-30T11:30:11-04:00</reporttime>
        <result>Negative</result>
        <units>10oz</units>
    </sample>
<sample>
        <site>40</site>
        <sampleid>040-2016-12388</sampleid>
        <productcode>AQUA</productcode>
        <productdescription>10ozAquafina</productdescription>
        <testcode>Oxygen</testcode>
        <testdescription>Test for Oxygen</testdescription>
        <sequence>0</sequence>
        <sampledate>2016-05-27T16:03:00-05:00</sampledate>
        <prefix></prefix>
        <addendum></addendum>
        <compositeid>78979</compositeid>
        <samplename>Water</samplename>
        <sampledescription>Tasty Water</sampledescription>
        <labsampleid>789-879717-002</labsampleid>
        <receivedby>John Doe</receivedby>
        <receivetime>2016-05-28T10:49:43-04:00</receivetime>
        <reporttime>2016-05-30T11:30:11-04:00</reporttime>
        <result>Negative</result>
        <units>10oz</units>
    </sample>
<sample>
        <site>40</site>
        <sampleid>040-2016-12389</sampleid>
        <productcode>AQUA</productcode>
        <productdescription>10ozAquafina</productdescription>
        <testcode>Oxygen</testcode>
        <testdescription>Test for Oxygen</testdescription>
        <sequence>0</sequence>
        <sampledate>2016-05-27T16:03:00-05:00</sampledate>
        <prefix></prefix>
        <addendum></addendum>
        <compositeid>78979</compositeid>
        <samplename>Water</samplename>
        <sampledescription>Tasty Water</sampledescription>
        <labsampleid>789-879717-003</labsampleid>
        <receivedby>John Doe</receivedby>
        <receivetime>2016-05-28T10:49:43-04:00</receivetime>
        <reporttime>2016-05-30T11:30:11-04:00</reporttime>
        <result>Positive</result>
        <units>10oz</units>
    </sample>
    <sample>
        <site>40</site>
        <sampleid>040-2016-78979</sampleid>
        <productcode>AQUA</productcode>
        <productdescription>10ozAquafina</productdescription>
        <testcode>Oxygen</testcode>
        <testdescription>Test for Oxygen</testdescription>
        <sequence>0</sequence>
        <sampledate>2016-05-27T16:03:00-05:00</sampledate>
        <prefix></prefix>
        <addendum></addendum>
        <compositeid></compositeid>
        <samplename>Water</samplename>
        <sampledescription>Tasty Water</sampledescription>
        <labsampleid></labsampleid>
        <receivedby>John Doe</receivedby>
        <receivetime>2016-05-28T10:49:43-04:00</receivetime>
        <reporttime>2016-05-30T11:30:11-04:00</reporttime>
        <result>Positive</result>
        <units>10oz</units>
    </sample>
</waterSample>

相关内容