Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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
[LanX]: safe?
[shmem]: LanX: you can pass a shell command via @ARGV.
[shmem]: moritz: don't know, but you'd prolly whip it up in no time.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2018-05-21 13:17 GMT
Find Nodes?
    Voting Booth?