what is the best way to find nodes a certain string (in this example:
/Find Me/) ? in the example below, i'm getting too much output - i'd
only expect 2 and 4. i had read there's a way of doing this with a
newer LibXML, but i was unable to get it to work nor find
documentation to suggest the perl module supports the newer xpath
features.

also, if there's a way to give LibXML a subref to parse with and i
could do something like:
my $xparse = sub {
 return if $_->textContent;=~ /Find Me/;
};
maybe i could do it that way?


# CODE

#!/usr/bin/perl

use strict;
use warnings;
#use Carp::Always;

use Data::Dumper;

use XML::LibXML;

my $xml_data = <<XML;
<?xml version="1.0" encoding="UTF-8"?>
<TEST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:noNamespaceSchemaLocation="null.xsd">
    <A>
        <B>Find Me</B>
        <C>Some Data</C>
    </A>
    <A>
        <B>Leave Me Alone</B>
        <C>Unimportant Data</C>
    </A>
    <A>
        <B>Find Me!</B>
        <C>Some More Data</C>
    </A>
</TEST>
XML

my $parser = XML::LibXML->new();
my $doc = $parser->parse_string($xml_data);

my $nodes = $doc->findnodes("//*[text()]");

my $i = 1;
foreach my $node (@$nodes) {
    print $i++ . " [" . $node->textContent . "]\n" if
$node->textContent =~ 'Find Me';
}

# OUTPUT

1 [

        Find Me
        Some Data


        Leave Me Alone
        Unimportant Data


        Find Me!
        Some More Data

]
2 [
        Find Me
        Some Data
    ]
3 [Find Me]
4 [
        Find Me!
        Some More Data
    ]
5 [Find Me!]

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to