0

我在使用 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 但是这个命令只是将两个文件合并为一个具有相同标题的文件。你有什么想法可以指导我解决这个问题吗?

4

1 回答 1

1

将两个 XML 文件相互附加。

文件 1.xml:

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

文件 2.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

于 2018-02-22T19:33:39.610 回答