ssr1012 ssr1012 - 2 months ago 7
Perl Question

How to avoid the attribute interchanging in xml while using XML::Twig in perl

In my input xml file we are doing some conversions work in perl. And we need to have the xml file with indentation. Hence we are using the Module

XML::Twig
(Simple way) for pretty indent.

In perl code:

XML::Twig->new( pretty_print => 'indented')
->parsefile( $input )
->print_to_file( $input);


Input:

<contrib contrib-type="author" id="U94028797" corresp="yes">


After doing this function the tool has been interchanging the attribute's in xml elements.

Output:

<contrib contrib-type="author" corresp="yes" id="U94028797">


Anyone give suggestion to avoid this and it would be appreciated. Thanks in advance.

Answer

Use the keep_atts_order option when you create the twig: docs

You will need Tie::IxHash to be installed.

Note that XML tools should not care at all about the order of the attributes.