How to parse an xml file in a shell script

text processingxml

I would like to know how can I split my data from the following format:

<datas>
 <data>
  <name>Name1</name>
 </data>
 <data>
  <name>Name2</name>
 </data>
</datas>

to the following format:

<data><name>Name1</name></data>
<data><name>Name2</name></data>

The parsed data would be sent to a Python script as follows:

 python script.py <data><name>Name1<name></data>
 python script.py <data><name>Name2<name></data>

I have tried commands like:

echo 'cat /datas/data' | xmllint --shell file.xml

but how can I pass the output in the desired format to the Python script?

Best Answer

I would preprocess the data with XMLStarlet:

$ xml sel -t -c '/datas/data' -nl data.xml
<data>
  <name>Name1</name>
 </data><data>
  <name>Name2</name>
 </data>

Then it depends on how you Python script wants to read this data. Hopefully, it's from a file or from standard input...

Related Question