Discussion:
xml find text with wildcard
shawn wilson
13 years ago
Permalink
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-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Gerhard
13 years ago
Permalink
[ ... ]
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 $nodes = $doc->findnodes("//*[text()]");
my $i = 1;
print $i++ . " [" . $node->textContent . "]\n" if $node->textContent =~ 'Find Me';
}
textContent
$content = $node->textContent;

this function returns the content of all text nodes in the
descendants of the given node as specified in DOM.

I guess you XPath is wrong, I've changed it to '//*/text()' and the result is
the following:

1 [Find Me]
2 [Find Me!]

Cheers,

Gerhard
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Continue reading on narkive:
Loading...