use XML::Parser;
my $indoctype = 0;
my $inroot = 0;
my $p = new XML::Parser(ErrorContext => 2,
Namespaces => 1,
ParseParamEnt => 1,
Handlers => {Start => \&sthndl,
End => \&endhndl,
Char => \&chrhndl,
Proc => \&proc,
Doctype => sub {$indoctype = 1},
DoctypeFin => sub {$indoctype = 0}
}
);
my $file = shift;
if (defined $file) {
$p->parsefile($file);
}
else {
$p->parse(*STDIN);
}
sub sthndl {
my $xp = shift;
my $el = shift;
$inroot = 1 unless $inroot;
my $ns_index = 1;
my $elns = $xp->namespace($el);
if (defined $elns) {
my $pfx = 'n' . $ns_index++;
print "<$pfx:$el xmlns:$pfx=\"$elns\"";
}
else {
print "<$el";
}
if (@_) {
for (my $i = 0; $i < @_; $i += 2) {
my $nm = $_[$i];
my $ns = $xp->namespace($nm);
$_[$i] = defined($ns) ? "$ns\01$nm" : "\01$nm";
}
my %atts = @_;
my @ids = sort keys %atts;
foreach my $id (@ids) {
my ($ns, $nm) = split(/\01/, $id);
my $val = $xp->xml_escape($atts{$id}, '"', "\x9", "\xA", "\xD");
if (length($ns)) {
my $pfx = 'n' . $ns_index++;
print " $pfx:$nm=\"$val\" xmlns:$pfx=\"$ns\"";
}
else {
print " $nm=\"$val\"";
}
}
}
print '>';
}
sub endhndl {
my ($xp, $el) = @_;
my $nm = $xp->namespace($el) ? "n1:$el" : $el;
print "</$nm>";
if ($xp->depth == 0) {
$inroot = 0;
print "\n";
}
}
sub chrhndl {
my ($xp, $data) = @_;
print $xp->xml_escape($data, '>', "\xD");
}
sub proc {
my ($xp, $target, $data) = @_;
unless ($indoctype) {
print "<?$target $data?>";
print "\n" unless $inroot;
}
}