Note that there are some explanatory texts on larger screens.

plurals
  1. POLibXML - Looping through nodes until
    primarykey
    data
    text
    <p>I'm trying to parse the below XML using Perl's XML::LibXML library.</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;TaggedPDF-doc&gt; &lt;Part&gt; &lt;Sect&gt; &lt;H4&gt;2.1 Study purpose &lt;/H4&gt; &lt;P&gt;This is study purpose content&lt;/P&gt; &lt;P&gt;content 1&lt;/P&gt; &lt;P&gt;content 2&lt;/P&gt; &lt;P&gt;content 3 &lt;/P&gt; &lt;P&gt;content 4&lt;/P&gt; &lt;P&gt;3. Some Header&lt;/P&gt; &lt;P&gt;obj content 4&lt;/P&gt; &lt;P&gt;obj content 2&lt;/P&gt; &lt;/Sect&gt; &lt;/Part&gt; &lt;/TaggedPDF-doc&gt; </code></pre> <p>For the Header Study Purpose, I'm trying to display all the related siblings. So my expected output is:</p> <pre><code>&lt;H4&gt;2.1 Study purpose &lt;/H4&gt; &lt;P&gt;This is study purpose content&lt;/P&gt; &lt;P&gt;content 1&lt;/P&gt; &lt;P&gt;content 2&lt;/P&gt; &lt;P&gt;content 3 &lt;/P&gt; &lt;P&gt;content 4&lt;/P&gt; </code></pre> <p>My Perl code is below. I can display the first node. </p> <p>Given a value of the first node,Study purpose, is there a way I can loop and print all the nodes until I hit a node containing a "digit followed by a '.'"?</p> <p>My perl implementation:</p> <pre><code>my $purpose_str = 'Purpose and rationale|Study purpose|Study rationale'; $parser = XML::LibXML-&gt;new; #print "Parser for file $file is: $parser \n"; $dom = $parser-&gt;parse_file($file); $root = $dom-&gt;getDocumentElement; $dom-&gt;setDocumentElement($root); for my $purpose_search('/TaggedPDF-doc/Part/Sect/H4') { $purpose_nodeset = $dom-&gt;find($purpose_search); foreach my $purp_node ($purpose_nodeset -&gt; get_nodelist) { if ($purp_node =~ m/$purpose_str/i) { #Get the corresponding child nodes @childnodes = $purp_node-&gt;nonBlankChildNodes(); $first_kid = shift @childnodes; $second_kid = $first_kid-&gt;nextNonBlankSibling(); #$third_kid = $second_kid-&gt;nextNonBlankSibling(); $first_kid -&gt; string_value; $second_kid -&gt; string_value; #$third_kid -&gt; string_value; } print "Study Purpose is: $first_kid\n.$second_kid\n"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload