Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Extracting specific childnodes (play xmllint --shell )

by Anonymous Monk
on Jul 06, 2013 at 22:42 UTC ( [id://1042949]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Extracting specific childnodes (xpath whitespace)
in thread Extracting specific childnodes

Why no response to Re^3: Extracting specific childnodes (xpath whitespace)?

If you're game, I'm game ; I'll play if you'll play;

Using the previous answer

$ xmllint.exe --shell madbee.xml / > / > cat //H4[ contains(.,'Include')] /..//LI ------- <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> / > / > / > cat //H4[ contains(.,'Include')] /..//LI_Title ------- <LI_Title>Critera 1</LI_Title> ------- <LI_Title>Critera 2</LI_Title> ------- <LI_Title>Critera 3</LI_Title> ------- <LI_Title>Critera 3</LI_Title> / > / > bye $

Debugging /diagnosing and fixing this latest typo-ed answer

$ xmllint.exe --shell madbee.xml / > / > cat //Article/Main/Sect/*[ ( name()="H3" or name()="H4" ) and cont +ains(.,"Include" ) ]//LI_Title / > / > whereis //Article/Main/Sect/*[ ( name()="H3" or name()="H4" ) and +contains(.,"Include" ) ]//LI_Title / > / > cat //Article/Main/Sect/*[ ( name()="H3" or name()="H4" ) and cont +ains(.,"Include" ) ] ------- <H4>Include</H4> / > / > whereis //Article/Main/Sect/*[ ( name()="H3" or name()="H4" ) and +contains(.,"Include" ) ] /Article/Main/Sect/H4 / > / > whereis //LI_Title /Article/Main/Sect/L/LI[1]/LI_Title /Article/Main/Sect/L/LI[2]/LI_Title /Article/Main/Sect/L/LI[3]/LI_Title /Article/Main/Sect/L/LI[4]/LI_Title / > / > / > cat //Article/Main/Sect/*[ ( name()="H3" or name()="H4" ) and cont +ains(.,"Include" ) ]/../LI_Title / > / > cat //Article/Main/Sect/*[ ( name()="H3" or name()="H4" ) and cont +ains(.,"Include" ) ]/..//LI_Title ------- <LI_Title>Critera 1</LI_Title> ------- <LI_Title>Critera 2</LI_Title> ------- <LI_Title>Critera 3</LI_Title> ------- <LI_Title>Critera 3</LI_Title> / > / > / > / > grep Include /Article/Main/Sect/H4 : tan 7 Include / > / > / > grep Critera /Article/Main/Sect/L/LI[1]/LI_Title : t-- 9 Critera 1 /Article/Main/Sect/L/LI[2]/LI_Title : t-- 9 Critera 2 /Article/Main/Sect/L/LI[3]/LI_Title : t-- 9 Critera 3 /Article/Main/Sect/L/LI[4]/LI_Title : t-- 9 Critera 3 / > / > / > bye $

Using the power of parens in xpath / parentheses in xpath

$ xmllint.exe --shell madbee.xml / > cat //Article/Main/Sect/H4 ------- <H4>Include</H4> / > / > / > cat ( //Article/Main/Sect/H3 | //Article/Main/Sect/H4 ) ------- <H4>Include</H4> / > / > / > cat ( //Article/Main/Sect/H3 | //Article/Main/Sect/H4 )[ contains( +.,'Include' ) ] ------- <H4>Include</H4> / > / > / > cat ( //Article/Main/Sect/H3 | //Article/Main/Sect/H4 )[ contains( +.,'Include' ) ]/..//LI_Title ------- <LI_Title>Critera 1</LI_Title> ------- <LI_Title>Critera 2</LI_Title> ------- <LI_Title>Critera 3</LI_Title> ------- <LI_Title>Critera 3</LI_Title> / > / > / > / > cat ( //Article/Main/Sect/H3 | //Article/Main/Sect/H4 )[ contains( +.,'Junk' ) ]/..//LI_Title / > / > / > / > bye $

You can even give node sets to xpath string functions like contains() so you don't have to go to parent(..) then find descendents(//)

$ xmllint.exe --shell madbee.xml / > / > / > whereis //Article/Main/Sect /Article/Main/Sect / > / > / > whereis //Article/Main/Sect[ ( H3 | H4 ) ] /Article/Main/Sect / > / > / > whereis //Article/Main/Sect[ contains( ( H3 | H4 ), 'Junk' ) ] / > / > whereis //Article/Main/Sect[ contains( ( H3 | H4 ), 'Include' ) ] /Article/Main/Sect / > / > whereis //Article/Main/Sect[ contains( ( H3 | H4 ), 'Include' ) ] +//LI_Title /Article/Main/Sect/L/LI[1]/LI_Title /Article/Main/Sect/L/LI[2]/LI_Title /Article/Main/Sect/L/LI[3]/LI_Title /Article/Main/Sect/L/LI[4]/LI_Title / > / > / > bye $

Replies are listed 'Best First'.
Re^4: Extracting specific childnodes (play xmllint --shell )
by madbee (Acolyte) on Jul 07, 2013 at 04:50 UTC

    Dude! Thanks much. Weekend where I am-so didnt get on it until now.In your earlier post, the typo or should I say omission was the full path to LI_Title. Adding that fixed my problem and filled the array.

    Thank you for taking the time to run the code and explain. Really appreciate it..As you can tell, newbie to both Perl and Xpath and learning on the fly.

    Regards,Madbee

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1042949]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-20 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found