gsiglet has asked for the wisdom of the Perl Monks concerning the following question:
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: XPATH DOM traverse html/xml (build your own)
by ikegami (Patriarch) on Dec 07, 2009 at 19:02 UTC | |
You can use other expressions for the path segments if you want. I kept it simple. And of course, you can do other things other than printing the path and element name. Update: Fixed bugs. | [reply] [d/l] [select] |
Re: XPATH DOM traverse html/xml (->nodePath)
by ikegami (Patriarch) on Dec 07, 2009 at 19:16 UTC | |
| [reply] [d/l] [select] |
Re: XPATH DOM traverse html/xml (fancy)
by ikegami (Patriarch) on Dec 07, 2009 at 20:20 UTC | |
ok, I can't leave this alone :) To get
instead of
requires a lot more work.
The code doesn't use the XML::LibXML::XPathContext object it creates, but I figured you'd need it if you're building xpaths. | [reply] [d/l] [select] |
by gsiglet (Acolyte) on Dec 07, 2009 at 20:43 UTC | |
| [reply] |
Re: XPATH DOM traverse html/xml
by mirod (Canon) on Dec 08, 2009 at 16:11 UTC | |
I don't know which module you use, but with XML::Twig you can use the xpath method like this, for example: perl -MXML::Twig -E'my $t=XML::Twig->parse( "my.xml"); foreach my $e ($t->descendants( "#ELT")) { say $e->xpath; }' | [reply] [d/l] |
by benizi (Hermit) on Dec 09, 2009 at 06:18 UTC | |
XML::Twig is one of the many things that keeps me using Perl. (It's one of the modules my personal 'M.pm' module loads -- 'M.pm' so I can do perl -MM -lwe ....) So, thanks again, mirod. The first thing that came to mind was: perl -MXML::Twig -E 'XML::Twig->new( twig_handlers => { _all_ => sub { say $_->xpath } })->parsefile(shift)'...which isn't so different from what you wrote. But, the following has the benefit of not requiring the loading of the entire file (correct?): perl -MXML::Twig -E 'XML::Twig->new( start_tag_handlers => { _all_ => sub { say $_->xpath } })->parsefile(shift)' | [reply] [d/l] [select] |
by mirod (Canon) on Dec 09, 2009 at 13:18 UTC | |
Absolutely, the way you wrote it is more efficient. I just went for the easiest way I could think of, not knowing enough about the OP's exact constraints. | [reply] |