How to change the order of the attributes of an XML element using Perl and XML::Twig -


i new xml::twig. want change order of attributes of <product> elements shown below.

input.xml

<?xml version="1.0" encoding="utf-8"?> <root>   <product markup="xml" type="books" id="book1">     <name>analysis</name>     <prize>$203</prize>     <subject>construct</subject>   </product>   <product markup="xml" type="books" id="book2">     <name>analysis</name>     <prize>$203</prize>     <subject>bio</subject>   </product> </root> 

i need this

<?xml version="1.0" encoding="utf-8"?> <root>   <product id="book1" markup="xml" type="books">     <name>analysis</name>     <prize>$203</prize>     <subject>construct</subject>   </product>   <product id="book2" markup="xml" type="books">     <name>analysis</name>     <prize>$203</prize>     <subject>bio</subject>   </product> </root> 

my code is:

use xml::twig;  $xml = xml::twig->new(   twig_handlers   => {     #'product' => sub {$_},     # (i don't know process)   },   pretty_print    => 'record',   output_encoding => 'utf-8',   keep_atts_order => 1, );  $xml->parsefile("input.xml");  $xml->purge;  

how can change this?

as mentioned in comments, weird request. you're trying wrong way ( or working tools don't implement xml fully).

in case, since xml::twig default outputs attribute in alphabetical order, , alphabetically id comes before markup, comes before type, looks don't have anything. remove keep_atts_order option, read , write file, voilĂ ! suspect other xml tools behave way too.


Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -