use strict; use XML::Twig; my %binop = qw(divide / power ^); my %down1 = qw(unit 1 math 1 apply 1); my %units = qw(joule J meter m); # Recurse through the ML tree, finding the binary operators. sub recurse { my ($e) = shift; my $tag = $e->gi; if (exists $binop{$tag}) { my $p1 = $e->next_sibling; my $p2 = $p1->next_sibling if defined($p1); recurse($p1) if defined($p1); print $binop{$tag}; recurse($p2) if defined($p2); } elsif (exists $down1{$tag}) { my $child = $e->first_child; recurse($child) if defined($child); } elsif ($tag eq "cn") { print $e->text; } elsif ($tag eq "ci") { my $txt = $e->text; if ($units{$txt}) { print $units{$txt}; } else { print $txt; } } else { print $tag; } } # Uncomment if you really don't want to see an infix operator for the power. # $binop{power} = ""; my $t= XML::Twig->new(); my $txt = q{ joule meter 2 }; $t->parse($txt); my $root= $t->root; recurse($root); print "\n";