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