Welcome to the Monastery, and please read Writeup Formatting Tips.

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

In reply to Re: how to write this condition in XML:TWIG by toolic
in thread how to write this condition in XML:TWIG by lilili07

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.