Re: XML::LibXML navigation



On 12 Jan 2007 at 17:06, Rob Dixon wrote:

Hi Rob,


In the sample date below your'll see some addresses with "DO NOT..."
in. I can locate them easily enough but I am struggling to navigate
back up the DOM to access the code so I can record the code with
faulty addresses.

Here my effort. Can anyone help me either to move backup up to the
right element node or catch the code node before I begin to loop
through the address line(s).

TIA,
Dp.


======= My Effort ==========
#!/usr/bin/perl


my $file = 'ADDRESS.XML';
open(FH,$file) or die "Can't open file $file: $!\n";

my $parser = XML::LibXML->new;
my $doc = $parser->parse_fh(\*FH);

my @codes = $doc->findnodes('//code');
my @lines = $doc->findnodes('//lines');

for (my $i = 0; $i < $#codes; ++$i) {
#print $codes[$i]->string_value, "\t";
my @add = $lines[$i]->childNodes;
for ( my $a = 1; $a <$#add; ++$a) {
if ($add[$a]->string_value =~ /\s+NOT\s+/) {
print $codes[$i]->string_value,": ",$add[$a]-
string_value,"\n";
}
}


If I understand you correctly then all you need is

my @results = $doc->findnodes('/dataroot/address[contains(lines/line, "DO NOT
USE")]');

foreach my $address (@results) {
my $code = $address->findvalue('code');
print $code, "\n";
}

which prints the code of all those addresses that have a line containing 'DO NOT
USE'. Is that what was required?


Yes ...and no. I guess I want to print out the 'code' for any address
so that I can get the data corrected but I guess I would also like to
remove those records at the /dataroot/address level so they don't
appear in the file.

i spent a lot of time on this today as this look like a excellent
parser and DOM navigator but I struggled moving around.

In your example @results looks like it would contain references to
all the /lines/line data with DO NOT USE in the string_value. What I
have struggling with is that this is also a reference to the record
as a whole and my navigation techniques are not working out. For
example whenever I used findnodes I was getting every code in the
file. I think now that was because I was using /dataroot/address as
the starting point.

Aside from CPAN, I would appreciate any other sources of info about
using the using libXML with perl and xpath expressions. It is
whoppingly fast.

Thanx again,
Dp.



Rob


.



Relevant Pages

  • Re: XML::LibXML navigation
    ... I can locate them easily enough but I am struggling to navigate ... > parser and DOM navigator but I struggled moving around. ... The XPath expression ...
    (perl.beginners)
  • Re: XML::LibXML navigation
    ... I can locate them easily enough but I am struggling to navigate back up the DOM to access the code so I can record the code with faulty addresses. ... foreach my $address { ... Using findnodes and specifying an XPath statement only seems to pick up the first line in the lines element. ...
    (perl.beginners)
  • Re: Java cross reference tool
    ... > Robert Klemme wrote: ... >> even navigate through std lib classes. ... > references when extra tags are added to the code. ... Kind regards ...
    (comp.lang.java.softwaretools)
  • MDB or ADP, whats that all about?
    ... When I try to navigate the Office Assistant in Access I frequently see two ... references to "help" on a particular search topic. ... Return email address is not as DEEP as it appears ...
    (microsoft.public.access.gettingstarted)
  • Re: Cycle Time if in a Specific Year
    ... If you are struggling to see what is happening in a formula, ... always break it down into manageable chunks until you can see what's going ... references in the formula, as you did for 2007 in L2] ... David Biddulph ...
    (microsoft.public.excel.worksheet.functions)