Python - lxml to re-order xml tags



I have some sections in my xml which I need to re-order, I know xml doesn't need re-ordering, but this is what I need to do, but can't figure out the 'correct' way to do it. I am using lxml and have been using the .insert command to re-order, but keep getting IndexError: list index out of range. I need to re-order each of the tags inside each <asset type="preview"> so that it looks like this:



<asset type="preview">
<territories>
<territory>SE</territory>
</territories>
<data_file role="source">
<locale name="es"/>
<file_name>some_name_nor-preview-sv.mov</file_name>
<size>1715119116</size>
<checksum type="md5">55cd94d051700be34014b2892e925fa1</checksum>
<attribute name="crop.top">25</attribute>
<attribute name="crop.bottom">25</attribute>
<attribute name="crop.left">4</attribute>
<attribute name="crop.right">4</attribute>
<attribute name="image.burned_subtitles.locale">sv</attribute>
<attribute name="image.textless_master">false</attribute>
</data_file>
</asset>


I sometime have multiple <asset type="preview">, sometimes not. Also sometimes each <asset type="preview"> does not contain all of the the tags listed here. Here is the section of xml I am trying to re-order as above.



<asset type="preview">
<data_file role="source">
<size>1657800204</size>
<file_name>some_name_nor-preview.mov</file_name>
<checksum type="md5">c61dfa7139ab04560cac41cf5ba8a1f2</checksum>
<locale name="es"/>
<attribute name="crop.top">25</attribute>
<attribute name="crop.right">4</attribute>
<attribute name="crop.bottom">25</attribute>
<attribute name="crop.left">4</attribute>
</data_file>
<territories>
<territory>WW</territory>
</territories>
<data_file role="notes">
<size>9642</size>
<file_name>some_name_nor-preview-notes.pdf</file_name>
<checksum type="md5">4d0dc3534cd1d0f9885afbfda9be8b71</checksum>
</data_file>
</asset>
<asset type="preview">
<data_file role="source">
<size>1715119116</size>
<file_name>some_name_nor-preview-sv.mov</file_name>
<checksum type="md5">55cd94d051700be34014b2892e925fa1</checksum>
<locale name="es"/>
<attribute name="image.burned_subtitles.locale">sv</attribute>
<attribute name="crop.top">25</attribute>
<attribute name="crop.right">4</attribute>
<attribute name="image.textless_master">false</attribute>
<attribute name="crop.left">4</attribute>
<attribute name="crop.bottom">25</attribute>
</data_file>
<territories>
<territory>SE</territory>
</territories>
</asset>
<asset type="preview">
<data_file role="source">
<size>1709158524</size>
<file_name>some_name_nor-preview-fi.mov</file_name>
<checksum type="md5">58c5fcfa718393f76cb9b2d8f7c10362</checksum>
<locale name="es"/>
<attribute name="crop.bottom">25</attribute>
<attribute name="crop.top">25</attribute>
<attribute name="crop.left">4</attribute>
<attribute name="image.textless_master">false</attribute>
<attribute name="crop.right">4</attribute>
<attribute name="image.burned_subtitles.locale">fi</attribute>
</data_file>
<territories>
<territory>FI</territory>
</territories>
</asset>
<asset type="preview">
<territories>
<territory>NO</territory>
</territories>
<data_file role="source">
<size>1718632572</size>
<file_name>some_name_nor-preview-no.mov</file_name>
<checksum type="md5">41734d9d8dd4165416a4369f4ce9c8e1</checksum>
<locale name="es"/>
<attribute name="crop.left">4</attribute>
<attribute name="crop.top">25</attribute>
<attribute name="crop.bottom">25</attribute>
<attribute name="image.textless_master">false</attribute>
<attribute name="image.burned_subtitles.locale">no</attribute>
<attribute name="crop.right">4</attribute>
</data_file>
</asset>
<asset type="preview">
<territories>
<territory>DK</territory>
</territories>
<data_file role="source">
<size>1721312028</size>
<file_name>some_name_nor-preview-da.mov</file_name>
<checksum type="md5">919abd17baf680161a220dbae8409918</checksum>
<locale name="es"/>
<attribute name="image.textless_master">false</attribute>
<attribute name="crop.bottom">25</attribute>
<attribute name="image.burned_subtitles.locale">da</attribute>
<attribute name="crop.right">4</attribute>
<attribute name="crop.left">4</attribute>
<attribute name="crop.top">25</attribute>
</data_file>
</asset>

No comments:

Post a Comment