Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hello! Still trying to parse the same XML as before - but now having trouble with siblings. I have 2 versions of the xml file. Need to come up with a generic script to extract the values in either cases. Pasting the XML first and then my attempt.

When I run my script, it doesnt return any siblings. just 1.

XML File: Version 1 <root> <part> <sect> <header> 1. Purpose and rationale <P>purpose 1</p> <p>Purpose 2</p> <p>purpose 3</p> <l> <li>purpose list 1</li> <li>list2</li> </l> </header> </sect> </part> </root>

Objective:In this scenario, I need to search for Purpose and rationale. If found, need to extract all siblings between <header> and </header>

The same section Purpose and rationale can exist in another xml but in different format. The challenge is to have 1 generic script to handle both scenarios.

XML Scenario 2:

<root> <part> <sect> <header> 1. Purpose and rationale <P>purpose 1</p> <p>Purpose 2</p> <p>purpose 3</p> <l> <li>purpose list 1</li> <li>list2</li> </l> <p>2. Some other heading</p> <p>content 1</p> <p>content 2</p> </header> </sect> </part> </root>

For scenario 2, I need to extract all the siblings under Purpose and rationale only until "Some other heading". This heading title can change. The only identifier is that the node begins with a number.

In this xml content from 2 headers is mixed up. so i only need to extract the content from the siblings of the "purpose and rationale" section

My attempted code is below:

my $dom = XML::LibXML->new->parse_file($file); my $study_str = 'Purpose and rationale|Study purpose|Study ration +ale' for my $search ('/root/part/sect/header') { my $nodeset = $dom->find($search); foreach my $node($nodeset -> get_nodelist) { $node -> string_value; if ($node =~ m/$study_str/i) { my $protocol = $node; print $protocol,"\n"; #go to the next sibling while ($node -> { Node }) { if ($node -> { Node } -> getNextSibling ) { $node -> { Node } = $node -> getNextSibling; return $node -> { Node }; } } } } }

This only returns the value within the header tags and none of the children. obviously,i'm doing something wrong.Hoping for some help here to extract the content I need.

Thanks again for your help and apologies if the question is not clear

Regards, Madbee


In reply to Having trouble with siblings by madbee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (9)
As of 2024-04-23 09:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found