in reply to set attributes to an element in user defined order
How you ever got that code to even compile with this line in it I don't get (quotes are missing and the semicolon should be a comma)
*[@att='type'] => \&changeTagAttrOrderH +andler;
But anyway, your problem might be that this line
makes an ordinary hash (not an IxHash) and passes it to the set_atts method, so the order of the elements is lost.$rootElt->set_atts( { $att1 => $att1Val, $att2 => $att2Val, $a +tt3 => $att3Val});
As a simple standalone example, this code gives the attributes in a fixed order but it doesn't if you comment out the line with the arrow
use warnings; use strict; use XML::Twig; use Tie::IxHash; my $tw = XML::Twig->new("keep_atts_order", 1); my %atts; tie %atts, Tie::IxHash::; # <----- %atts = ("year", 2010, "month", 3, "day", 5); $tw->set_root(XML::Twig::Elt->new("date", \%atts)); $tw->flush; print "\n"; __END__
Alternately use the set_att method like this:
(But note that set_att does not delete the existing attributes, so you may need to call del_atts first.)use warnings; use strict; + use XML::Twig; + my $tw = XML::Twig->new("keep_atts_order", 1); $tw->set_root(my $de = XML::Twig::Elt->new("date")); $de->set_att("year", 2010, "month", 3, "day", 5); $tw->flush; print "\n"; __END__
|
|---|