Обновление двух xml-файлов с использованием xmlstarlet
У меня есть проблема с обновлением XML-файла данными второго XML-файла с помощью xmlstarlet. Например, у меня есть первый 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 ответ
Решение
Добавьте два 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>
Увидеть: xmlstarlet
, xmlstarlet edit
а также xmlstarlet select