in reply to Re: print specific XML node number
in thread print specific XML node number
You are 100% right about your assumption "If the contents of <element1> match in the 2 documents, you want to print the enclosing <elements>...</elements>. Of both documents? Or just one?" .... Im trying to print both
let me be more specific I have previously done this, Which is printing nodes im interested in regardless of match or miss match.
Now Im interested in both match and the whole node that has this specific <element1> text match. Please read my comments to get a feeling of what im trying to do
my $parser = XML::LibXML->new(); my $doc = $parser->parse_file("main_file.XML"); my $doc1 = $parser->parse_file("second_file.XML"); #get my text my @nodes_from_main= $doc0->findnodes('//path/to/element/text()'); my @nodes_from_second = $doc1->findnodes('//path/to/element/text()'); #get the node containing the previous text i.e containing <element1> my @node_I_need_from_main= $doc0->findnodes('//path/to/<elements>'); my @node_I_need_from_second = $doc1->findnodes('//path/to/<elements>)' +); #Note that main is unique meaning it will have only one instant from t +he text im matching #where my second, third, fourth....etc. will or might have more than o +ne match #now im trying to find the match which is working fine my $i=0; for my $node_main (@nodes_from_main) { #print my node from the main file, next look for matches #the problem is im looping through nodes at //path/to/element/text( +) #dont know how to loop through '//path/to/<elements>' at the same t +ime #or change my path at this particular instant for my $node_second (@nodes_from_second) { if (text from main equal text from second, "which I did already") { print my matching node from the second file, same issue I discu +ssed in the above comment. Im looking for something like this: print $node_I_need_from_second [$i] (matching node) or any other way to get the same result i.e print node: <elements> <element1>matching text</element1> <element2>other text</element2> <element3>other text</element3> </elements> } esle { #do nothing; } ++$i; } }
I hope this gives a much more clear idea about my problem...An alternative would be an Xpath expression something like "find all nodes where <element1> = $value"
this will actually this gives me a 2 in one solution
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: print specific XML node number
by zak_s (Initiate) on Dec 11, 2013 at 17:51 UTC |