in reply to how to write this condition in XML:TWIG
If I correctly deciphered your XML, then you are probably looking for something like:
use strict; use warnings; my $xml = <<XML; <?xml version="1.0" encoding="utf-8"?> <root> <class name="music"> <record type="month" index="0"> <string>sam1</string> <string>sam2</string> </record> <record type="month" index="1"> <string>mike1</string> <string>mike2</string> </record> </class> <class name="english"> <record type="week" index="2"> <string>luke1</string> <string>luke2</string> </record> <record type="month" index="3"> <string>ben1</string> <string>ben2</string> </record> </class> </root> XML use XML::Twig; my $t = XML::Twig->new(twig_handlers => {class => \&class}); $t->parse($xml); sub class { my ($twig, $cl) = @_; print "class name = ", $cl->att('name'), "\n"; for my $rec ($cl->children('record')) { print "type = ", $rec->att('type'); print ", index = ", $rec->att('index'), "\n"; } } __END__ class name = music type = month, index = 0 type = month, index = 1 class name = english type = week, index = 2 type = month, index = 3
|
|---|