使用xmlstarlet更新两个xml文件

时间:2018-02-22 17:35:12

标签: xml xmlstarlet xmlupdate

使用xmlstarlet更新包含第二个xml文件数据的xml文件时遇到问题。 例如,我有一个这种类型的第一个xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <timestamp></timestamp>
  <results>
    <idFile>1</dFile>
    <activated>true</activated>
    <id>9159820</id>
    <content>
      <resultDate>25/02/2018 19:33</resultDate>
      <presDate>01/01/2018</prescriptionDate>
      <seen>false</seen>
      <prescriberName>BABA</prescriberName>
      <labId>789</labId>
      <labName>Bio</laName>
      <checkedResults>
        <id>177331580</id>
        <localCode>GLU</localCode>
        <localLabel>GLU</localLabel>
        <Code>17856-6</Code>
        <value>IN</value>
        <unit>%</unit>
        <normalLow>4.0</normalLow>
        <normalHeight>6.0</normalHeight>
        <Label>BABA</Label>
      </checkedResults>
          <checkedResults>
        <id>177331580</id>
        <localCode>GLU</localCode>
        <localLabel>GLU</localLabel>
        <Code>17856-6</Code>
        <value>IN</value>
        <unit>%</unit>
        <normalLow>4.0</normalLow>
        <normalHeight>6.0</normalHeight>
        <Label>BABA</Label>
      </checkedResults>
    </content>
  </results>
</root>

还有第二个文件

<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <timestamp></timestamp>
  <results>
    <idFile>2</dFile>
    <activated>true</activated>
    <id>9159810</id>
    <content>
      <resultDate>25/02/2018 19:33</resultDate>
      <presDate>01/01/2018</prescriptionDate>
      <seen>false</seen>
      <prescriberName>BABA</prescriberName>
      <labId>789</labId>
      <labName>Bio</laName>
      <checkedResults>
        <id>177331580</id>
        <localCode>GLU</localCode>
        <localLabel>GLU</localLabel>
        <Code>17856-6</Code>
        <value>IN</value>
        <unit>%</unit>
        <normalLow>4.0</normalLow>
        <normalHeight>6.0</normalHeight>
        <Label>BABA</Label>
      </checkedResults>
          <checkedResults>
        <id>177331580</id>
        <localCode>GLU</localCode>
        <localLabel>GLU</localLabel>
        <Code>17856-6</Code>
        <value>IN</value>
        <unit>%</unit>
        <normalLow>4.0</normalLow>
        <normalHeight>6.0</normalHeight>
        <Label>BABA</Label>
      </checkedResults>
    </content>
  </results>
</root>

我想要的是这样的东西(我只需要用第二个文件的元素结果更新(添加)第一个文件):

<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <timestamp></timestamp>
  <results>
    <idFile>1</dFile>
    <activated>true</activated>
    <id>9159810</id>
    <content>
      <resultDate>25/02/2018 19:33</resultDate>
      <presDate>01/01/2018</prescriptionDate>
      <seen>false</seen>
      <prescriberName>BABA</prescriberName>
      <labId>789</labId>
      <labName>Bio</laName>
      <checkedResults>
        <id>177331580</id>
        <localCode>GLU</localCode>
        <localLabel>GLU</localLabel>
        <Code>17856-6</Code>
        <value>IN</value>
        <unit>%</unit>
        <normalLow>4.0</normalLow>
        <normalHeight>6.0</normalHeight>
        <Label>BABA</Label>
      </checkedResults>
          <checkedResults>
        <id>177331580</id>
        <localCode>GLU</localCode>
        <localLabel>GLU</localLabel>
        <Code>17856-6</Code>
        <value>IN</value>
        <unit>%</unit>
        <normalLow>4.0</normalLow>
        <normalHeight>6.0</normalHeight>
        <Label>BABA</Label>
      </checkedResults>
    </content>
  </results>
  <results>
    <idFile>2</dFile>
    <activated>true</activated>
    <id>9159810</id>
    <content>
      <resultDate>25/02/2018 19:33</resultDate>
      <presDate>01/01/2018</prescriptionDate>
      <seen>false</seen>
      <prescriberName>BABA</prescriberName>
      <labId>789</labId>
      <labName>Bio</laName>
      <checkedResults>
        <id>177331580</id>
        <localCode>GLU</localCode>
        <localLabel>GLU</localLabel>
        <Code>17856-6</Code>
        <value>IN</value>
        <unit>%</unit>
        <normalLow>4.0</normalLow>
        <normalHeight>6.0</normalHeight>
        <Label>BABA</Label>
      </checkedResults>
          <checkedResults>
        <id>177331580</id>
        <localCode>GLU</localCode>
        <localLabel>GLU</localLabel>
        <Code>17856-6</Code>
        <value>IN</value>
        <unit>%</unit>
        <normalLow>4.0</normalLow>
        <normalHeight>6.0</normalHeight>
        <Label>BABA</Label>
      </checkedResults>
    </content>
  </results>
</root>

到目前为止我尝试过:

使用命令xmlstarlet ed 1.xml 2.xml > 3.xml 但是此命令仅将两个文件合并为一个具有相同标头的文件。你有什么想法指导我这个问题吗?

1 个答案:

答案 0 :(得分:1)

相互附加两个XML文件。

file1.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <timestamp></timestamp>
  <results>
    <idFile>1</idFile>
    <activated>true</activated>
  </results>
</root>

file2.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <timestamp></timestamp>
  <results>
    <idFile>2</idFile>
    <activated>true</activated>
  </results>
</root>

使用xmlstarlet:

hull=$(xmlstarlet edit --delete '//root/results' file1.xml)
core1=$(xmlstarlet select --template -c '//root/results/*' file1.xml)
core2=$(xmlstarlet select --template -c '//root/results/*' file2.xml)

echo "$hull" \
  | xmlstarlet edit --subnode '//root' --type elem -n "results" --value "$core1" \
  | xmlstarlet edit --subnode '//root' --type elem -n "results" --value "$core2" \
  | xmlstarlet unescape \
  | xmlstarlet format

输出:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <timestamp/>
  <results>
    <idFile>1</idFile>
    <activated>true</activated>
  </results>
  <results>
    <idFile>2</idFile>
    <activated>true</activated>
  </results>
</root>

请参阅:xmlstarletxmlstarlet editxmlstarlet select