$xml = <<'*END*'
...
...
...
...
...
...
...
...
...
...
...
...
...
...
*END*
%rules = (
_default => sub {$_[0] => $_[1]->{_content}},
# by default we are only interested in the content and we want
# the parent to access it as an attribute of the same name as was the tag
bogus => undef, # means "ignore"
address => sub {address => "$_[1]->{street}, $_[1]->{city} ($_[1]->{country})"},
# let's convert the address to a single string
person => sub {'@person' => "$_[1]->{lname}, $_[1]->{fname}\n<$_[1]->{email}>\n$_[1]->{address}"}
# push the stringified data into the @{$parent->{person}}
doc => sub { join( "\n\n", @{$_[1]->{person}})}
);
print XML::TransformRules::Parse( $xml, \%rules);
####
$xml = <<'*END*'
...
...
...
...
...
...
...
123-456-7890
663-486-7890
663-486-7000
...
...
...
...
...
...
...
663-486-7891
*END*
%rules = (
_default => sub {$_[0] => $_[1]->{_content}},
bogus => undef,
address => sub {address => "$_[1]->{street}, $_[1]->{city} ($_[1]->{country})"},
phone => sub {$_[1]->{type} => $_[1]->{content}},
# let's use the "type" attribute as the key and the content as the value
phones => sub {delete $_[1]->{_content}; %{$_[1]}},
# remove the text content and pass along the type => content from the child nodes
person => sub { # lets print the values, all the data is readily available in the attributes
print "$_[1]->{lname}, $_[1]->{fname} <$_[1]->{email}>\n";
print "Home phone: $_[1]->{home}\n" if $_[1]->{home};
print "Office phone: $_[1]->{office}\n" if $_[1]->{office};
print "Fax: $_[1]->{fax}\n" if $_[1]->{fax};
print "$_[1]->{address}\n\n";
return; # the tag is processed, no need to remember what it contained
},
);