10.2 Transforming XML 255
use XML::Parser;
$p = new XML::Parser(Handlers => { Start => \&start });
print("
$p->parsefile($ARGV[0]);
print("
sub start {
$tag = $[1];
%attributes = @;
if ($tag eq "Interview") {
print(" <Interview");
print(" Date=’$attributes{Date}’");
$WeightUS = $attributes{Weight} 2.2;
print(" Weight=’$WeightUS’");
$HeightUS = $attributes{Height} 0.39;
print(" Height=’$HeightUS’");
print("/>\n");
}
}
Program 10.24 Transforming XML attributes
The most efficient technique for transforming from XML to XML is to
use the handlers style. Consider the task of reading the health study data-
base and changing the height and weight from centimeters and kilograms to
inches and pounds. The input document looks like this:
...
The output document should contain only the date, height, and weight at-
tributes of each interview. The result of running program 10.24 using the
example database is