Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Counting number of child nodes based on element value

by madbee (Acolyte)
on Jul 05, 2013 at 06:58 UTC ( #1042617=perlquestion: print w/ replies, xml ) Need Help??
madbee has asked for the wisdom of the Perl Monks concerning the following question:

Hello!This is in reference to: Title:XML parsing and Lists

For the below xml file: <Aritcle> <Main> <Sect> <H4>Include</H4> ..... <P1> This is the criteria</P1> <L> <LI> <LI_Label>1.</LI_Label> <LI_Title>Critera 1</LI_Title> </LI> <LI> <LI_Label>2.</LI_Label> <LI_Title>Critera 2</LI_Title> </LI> <LI> <LI_Label>3.</LI_Label> <LI_Title>Critera 3</LI_Title> </LI> <LI> <LI_Label>4.</LI_Label> <LI_Title>Critera 3</LI_Title> </LI> </Sect> </Main> </Article>

I need to get the count of LI elements where H4 = "include". The document I am working with can have multiple Sections and headers. But only for Header = "Include", I need to get the total count of LI elements only.

From the referenced link, I understand we can do this using XpathContext: xdom->find(count(path));.

However, I am not sure how I could filter it by the value of the Header element. Is it possible in 1 XPath expression? If so, can someone please give an example of how this can be done or point me to an example where this has been done already?

Thanks so much in advance.

madbee

Comment on Counting number of child nodes based on element value
Download Code
Re: Counting number of child nodes based on element value
by sahil2588 (Novice) on Jul 05, 2013 at 08:00 UTC
    You can achieve this easily by playing with data structures like hash of hashes. You can match where key H4 equals to "include" and by iterating loop you can count L1 elements. Or you can use "XML::Simple" module. and using "$xs->XMLin($xml);" will give you more readable form of your data. I hope that would be easy.
      XML::LibXML is about eleven thousand ways more advanced than XML::Simple
Re: Counting number of child nodes based on element value
by sahil2588 (Novice) on Jul 05, 2013 at 08:11 UTC

    something like this mike

    $XML = XML::LibXML->load_xml( string => $xml_string # parser options ... );
Re: Counting number of child nodes based on element value (typos)
by Anonymous Monk on Jul 05, 2013 at 08:22 UTC

    In Re^2: XML parsing and Lists you wrote a query using more than one tagname which appears nowhere in the xml you posted

    H5 is not H4, there is no "Part"

    $ xmllint.exe --xpath " //Article//Sect//H4[ contains(.,'Include')] /. +.//LI" madbee.xml <LI> <LI_Label>1.</LI_Label> <LI_Title>Critera 1</LI_Title> </LI><LI> <LI_Label>2.</LI_Label> <LI_Title>Critera 2</LI_Title> </LI><LI> <LI_Label>3.</LI_Label> <LI_Title>Critera 3</LI_Title> </LI><LI> <LI_Label>4.</LI_Label> <LI_Title>Critera 3</LI_Title> </LI> $ xmllint.exe --xpath " count( //Article//Sect//H4[ contains(.,'Includ +e')] /..//LI )" ma dbee.xml 4 $ xmllint.exe --xpath " count( //H4[ contains(.,'Include')] /..//LI )" + madbee.xml 4 $

    My advice is switch to font DejaVu Sans Mono, say size 11 or 12 or 20 , helps with the detailed oriented nature of programming

     [id://1042546] -> XML parsing and Lists

    what you posted still isn't valid xml -- XML::LibXML rejects it

    madbee2.xml:24: parser error : Opening and ending tag mismatch: L line + 7 and Sect </Sect> ^ madbee2.xml:25: parser error : Opening and ending tag mismatch: Sect l +ine 3 and Main </Main> ^ madbee2.xml:26: parser error : Opening and ending tag mismatch: Main l +ine 2 and Article </Article> ^ madbee2.xml:28: parser error : Premature end of data in tag Aritcle li +ne 1 ^

Log In?
Username:
Password:

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

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

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








    Results (254 votes), past polls