in reply to XML::Twig - Using xpath with twig roots

As documented, Twig does not support full XPath for handlers. Either create a flag that prevents processing of the 2nd and following occurences, or use a different module to handle XML that supports XPath (as XML::LibXML::Reader).
Update :An example using my favourite XML handling module, XML::XSH2:
open 944240.xml ; echo /bitrate[@name="1000"]/track[@type="V"]/range[1]/rangeStartTime ;

Replies are listed 'Best First'.
Re^2: XML::Twig - Using xpath with twig roots
by Anonymous Monk on Dec 19, 2011 at 14:38 UTC
    Thanks. Does findnodes() also not support the full Xpath? I see references that it does, but somehow it doesnt work for me.

      According to this post, findnodes() uses the full XPath engine. Also, I noticed that the use of position selectors like range[1] isn't supported on twig_handlers or twig_roots.

      Since you're dealing with a very, very small XML, I tried this and returned both rangeStartTimes. I cheated:)...
      #!/usr/bin/perl -l use strict; use warnings; use XML::Twig::XPath; $|=1; my $file = '/root/Desktop/bitrate.xml'; my $twig = XML::Twig::XPath->new( twig_roots => { "//range/rangeStartTime" => \&update }); $twig->parsefile($file); sub update { my ($twig, $server) = @_; print $server->text; }
      Here's the tidied XML:
      <?xml version="1.0" encoding="UTF-8"?> <bitrate name="1000"> <track type="V"> <range> <rangeStartTime>1261287</rangeStartTime> <rangeEndTime>1324271</rangeEndTime> </range> <range> <rangeStartTime>1324282</rangeStartTime> <rangeEndTime>0.000</rangeEndTime> </range> </track> </bitrate>
      Update: fixed typo