Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

XML::Twig question

by anniyan (Monk)
on Nov 24, 2005 at 10:51 UTC ( #511398=perlquestion: print w/ replies, xml ) Need Help??
anniyan has asked for the wisdom of the Perl Monks concerning the following question:

Yesterday i asked a question xml::Twig partial_path question regarding path depth, and i got the answer in which they said to use something like this $epath = '<chap/<ch/<sec//<anchor'; , but i am getting error. where am i going wrong?

use XML::Twig; $str ='<chap> <ch> <sec> <p> <anchor>THE ILIAD</anchor> <author>HOMER</author> </p> </sec> <sec> <p> <anchor>BOOK I</anchor> <para> Sing, O goddess, the anger of Achilles son of Peleus, that brought countless ills upon the Achaeans. </para> </p> </sec> </ch> </chap> '; $epath = '<chap/<ch/<sec//<anchor'; $relt = 'anchors'; $epath =~ s/<//g; my $twig = XML::Twig->new( twig_handlers => { $epath => sub { normal (@_, $relt)}, pretty_print => 'indented' ); $twig->parse("$str"); #$twig->print; sub normal { my ($twig, $elt, $relt) = @_; $elt->set_gi( "$relt" ); #$elt->print; }
Error: unrecognized expression in handler: 'chap/ch/sec//anchor' at D:\ver\co +nversion.pl line 132

Regards,
Anniyan
(CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

Comment on XML::Twig question
Select or Download Code
Re: XML::Twig question
by mirod (Canon) on Nov 24, 2005 at 11:02 UTC

    As per the error message: the expression is not valid. The '//' is not recognized in the expression (see twig_handlers syntax). Is there any reason why you don't use just plainanchor?

    Oh, and why the monkeying with '<' in the expression?

      mirod If you go through my yesterday's posting you can able to know my requirement. I want to avoid giving full path, i just want to give only the starting tags and ending tags with any depth inbetween. How to do that? Also as per my ini file i am getting the input with '<' symbol, but i will replace that before passing it to twig handler.

      Regards,
      Anniyan
      (CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

        Honestly, I can't think of too many cases where being able to specify the top and lowest level tags only would make much sense. So maybe a more realistic example, or a quick explanation of why you need to do this, would probably get a more relevant answer.

        Anyway, in general, when you get to the limit of the mini-language used to declare handlers, the solution is to "finish the job" in the handler itself. So filter first the regular way, and test the rest of the condition at the begining of the handler, returning if it is not met.

        So you would have for example:

        my $twig= XML::Twig->new( twig_handlers => { title => \&title }) ->parsefile( "foo.xml"); sub title { my( $twig, $title)= @_; # return 1 (true) if you can have other handlers on the # title, otherwise it doesn't matter what you return # see the doc for twig_handlers return 1 unless( $title->root->matches( 'gutbook'); $title->set_tag( 'tit'); }

        Does it help?

Re: XML::Twig question
by murugu (Curate) on Nov 24, 2005 at 11:50 UTC
    Hi anniyan,

    If you really want to use XPATH expression to be evaluated then use method get_xpath .

    'ELT1//ELT2' will find ELT2 elements which are all descendant element of ELT1, when given inside get_xpath method.

    There is no need for '<' in the XPATH expression.

    Hope this helps.

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://511398]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (18)
As of 2014-07-14 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (260 votes), past polls