RE: Counting specific elements in a XML object



How about:

cat fn | grep <string to be searched for> | wc

as a non-Perl approach to the problem...

-----Original Message-----
From: Hans Meier (John Doe) [mailto:security.department@xxxxxxxx]
Sent: Thursday, March 30, 2006 11:38 AM
To: beginners@xxxxxxxx
Subject: Re: Counting specific elements in a XML object

Dave Adams am Donnerstag, 30. März 2006 21.12:
If I have a xml file like the following:

<?xml version='1.0'?>
<employee>
<name>John Doe</name>
<age>43</age>
<sex>M</sex>
<department>Recieving</department>
</employee>
<employee>
<name>Bob Gordon</name>
<age>50</age>
<sex>M</sex>
<department>Shipping</department>
</employee>


Is there some perl module out there that can help me get the number of
employees or in other words, the number occurences of "<employee>"?

I guess I can get it conventionally by reading in the file and doing a
count every time the script encounters "<name>". But there must be an
easier way.

Ok, let's start with:

perl -e 'while(<STDIN>){/<employee>/&&$i++};print $i' < data.xml

Anybody with an easier way in the sense of
- less typing
- more understandable
- less module imports ;-)
?

[preconditions: nothing else to do with the input; no CDATA containing
<employees>; valid XML; etc.]

Hans



--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
<http://learn.perl.org/> <http://learn.perl.org/first-response>



.



Relevant Pages

  • Re: Counting specific elements in a XML object
    ... Is there some perl module out there that can help me get the number of ... [preconditions: nothing else to do with the input; no CDATA containing ...
    (perl.beginners)
  • Re: How to count records w/o counting duplicates by week, month, or ye
    ... How about using something like this as your base query. ... If I do a simle count of employee ID, I get 2x the number of real employees. ... The hours and salary cost is not an issue, but counting the first EmpID in the month, and then not counting it until the next month is killing me! ...
    (microsoft.public.access.queries)
  • RE: How to count records w/o counting duplicates by week, month, or ye
    ... Another method would be to create a normalizing query, to get your data in a ... added ability of summing hours by month, not the month of the WeDte. ... employee ID, I get 2x the number of real employees. ... then not counting it until the next month is killing me! ...
    (microsoft.public.access.queries)
  • RE: How to count records w/o counting duplicates by week, month, or ye
    ... employee ID, I get 2x the number of real employees. ... year to get the total hours, salary cost, and employee count. ... salary cost is not an issue, but counting the first EmpID in the month, and ... then not counting it until the next month is killing me! ...
    (microsoft.public.access.queries)
  • Re: Excel formula question
    ... George 2-feb ... If employee calls off on Feb 2 and calls again on Feb 3, ... I am looking for a formula to automate the counting of occurences. ...
    (microsoft.public.excel.programming)