Reputation: 73
THE SITUATION
After I have edited an XML and changed the values successfully, I need to write the changes back to the file. However, when I do this, the first tag of the XML, which has two attributes (a) date_time and (b) xmlns:xsi gets switched around after the uodate.
The following is the sample of the XML:
XML Document sample
THE ORIGINAL XML
<?xml version="1.0" encoding="UTF-8"?>
<basenet date_time="201202131140" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<msg_ver>01</msg_ver>
<sender_id>1234</sender_id>
<recipient_id>Bob</recipient_id>
</basenet>
The Output
<?xml version="1.0" encoding="UTF-8"?>
<tradenet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" date_time="201202131140">
<msg_ver>01</msg_ver>
<sender_id>1234</sender_id>
<recipient_id>Bob</recipient_id>
</tradenet>
The Code
sub CHANGE_DATE_SEQ_NUM()
{
# load
open my $fh, '<', $newfile;
binmode $fh; # drop all PerlIO layers possibly created by a use open pragma
my $doc = XML::LibXML->load_xml(IO => $fh);
my $query = "/tradenet/message/header/unique_ref_no/date/text( )";
my $query2 = "/tradenet/message/header/unique_ref_no/seq_no/text( )";
my $query3 = "/tradenet/message/transport/out_tpt/dept_date/text( )";
#print $query3;
my($node) = $doc->findnodes($query);
my($node2) = $doc->findnodes($query2);
my($node3) = $doc->findnodes($query3);
$node->setData("$date");
$node2->setData("$file_seq_number");
$node3->setData("$date");
# save
open my $out, '>', $newfile;
binmode $out; # as above
$doc->toFH($out);
}
My Question Why does the date_time and xmlns:xsi in the tag get switched? The switch is causing my XML to fail the validation at my server side.
Thank you for you time.
Upvotes: 0
Views: 223
Reputation: 385764
Attribute order is meaningless in XML, so the bug is in your validation code.
It probably would have been less efficient for XML::LibXML or underlying libxml2 to preserve the order, so they didn't, seeing as there's no reason to do so. The attributes are probably stored in a hash table.
Upvotes: 1