1$|=1;
2use XML::Rules;
3
4$xml = <<'*END*';
5<school>
6
7<some>tag</some>
8
9<classes name="Primary">
10<student name="Junkman">
11<Age>12</Age>
12</student>
13
14<student name="Lotman">
15<Age>14</Age>
16</student>
17</classes>
18
19<classes name="Nursery">
20<student name="Testman">
21<Age>34</Age>
22</student>
23</classes>
24
25<classes name="SomeClass">
26</classes>
27<other><tags>with values</tags></other>
28</school>
29*END*
30
31my $parser = new XML::Rules (
32  rules => [
33    _default => 'raw',
34    classes => sub {
35      my ($tag, $attrs, $context, $parents, $parser) = @_;
36      my $add = $parser->{parameters}{$attrs->{name}};
37
38      if ($add) {
39        if (ref($attrs->{_content})) {
40          push @{$attrs->{_content}}, @$add
41        } else {
42          $attrs->{_content} = [ $attrs->{_content}, @$add];
43          # there were no students in the class, the tag contained only the whitespace
44        }
45      }
46      return $tag => $attrs; # the module will print the branch
47    },
48  ],
49  style => 'filter', # the default is : style => 'parser'
50);
51
52open my $OUT, '>', 'filter.txt';
53$parser->filterstring($xml => $OUT,
54	{
55		SomeClass => [
56			[student => {name => 'Johny', age => {_content => 31}}], "\n",
57		],
58		Nursery => [
59			[student => {name => 'Paul', age => {_content => 36}}], "\n",
60			[student => {name => 'Martin', age => {_content => 33}}], "\n",
61		],
62	}
63);
64close $OUT;