Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Extracting specific childnodes

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

Hello! I'm trying to parse an XML for which I have to find an extract only specific childnodes. The XML file is below:

<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>
From the above XML file, I need to extract only the LI_Title nodes where Header = "Include". There can be 1 or many LI_Titles in an xml file and many such sections

I've come up with the following to identify the nodes.But I'm not sure how I can just pull out the LI_Title nodes.So, hoping for some help here.

$dom = $parser->parse_file($file); my $expr = ('//Article//Main//Sect//H3[contains(.,"Include")]|//Articl +e//Main//Sect//H4[contains(.,"Include")]|//Article//Main[contains(.," +Include")]//*[name()="LI"]'); my @nodes=$dom->findnodes($expr); foreach $nod(@nodes) { print "element: ".$nod->nextNonBlankSibling(); }

Thanks in advance.-madbee

Replies are listed 'Best First'.
Re: Extracting specific childnodes (xpath whitespace)
by Anonymous Monk on Jul 06, 2013 at 03:19 UTC

      Hello @Anonymous Monk! Thanks for responding. Tried your approach. It didn't return anything.

      @nodes=$dom->findnodes($xpath_Include_LI_Title); print "{@nodes}\n";

      the nodes array should contain the values of LI_Title elements, correct?

      Thanks again!

        Well, I made a typo of omission . I didn't make the same typo one of the linked nodes. Can you figure out what it is? What is the first step to figure it out?

        Can you explain in english the xpath I provided in untypo'd node?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1042837]
Approved by sundialsvc4
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2017-08-17 08:54 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (285 votes). Check out past polls.