Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

XML::LibXML count nodes

by elgato (Novice)
on Nov 17, 2011 at 15:23 UTC ( #938632=perlquestion: print w/ replies, xml ) Need Help??
elgato has asked for the wisdom of the Perl Monks concerning the following question:

Hi. It there any good way to get number of found nodes on xpath query? Smth like this:
my $doc = $parser->parse_file($file); print length(@{$doc->findnodes('//category')})
I just need nodes count without making foreach counter (have many rows). Thanks in advance.

Comment on XML::LibXML count nodes
Download Code
Replies are listed 'Best First'.
Re: XML::LibXML count nodes
by choroba (Canon) on Nov 18, 2011 at 08:44 UTC
    Use XPath function count:
    my $count = $doc->findvalue("count(//category)");
Re: XML::LibXML count nodes
by Anonymous Monk on Nov 17, 2011 at 15:27 UTC
      Seems like what i've needed! Thanks! PS: LibXML docs suck =\
Re: XML::LibXML count nodes
by Lotus1 (Chaplain) on Nov 17, 2011 at 19:10 UTC

    Try this:

    print scalar(@{$doc->findnodes('//category')});

    I've done it by assigning the nodes to an array and then just printing the number of elements in the array.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2015-07-29 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls