$doc->findnodes('foo')
should be
$doc->findnodes('seg/foo')
or
$doc->findnodes('/seg/foo')
Problem #2: You ask for the text children of the foo element, but it doesn't have any. It's not even text you want!
$foo->findnodes('text()')
should be
join('', map { $_->toString() } $foo_node->childNodes)
which can be simplified to
join('', $foo_node->childNodes)
Also,
$foo->findvalue('@mid')
is better written as
$foo->getAttribute('mid')
So, we get
use strict;
use warnings qw( all );
use feature qw( say );
use XML::LibXML qw( );
my $xml = <<'__EOS__';
<seg><foo mid="0" mtype="seg"><g id="1">Need to export this text</g></
+foo></seg>
__EOS__
my $doc = XML::LibXML->new->parse_string($xml);
for my $foo_node ($doc->findnodes('/seg/foo')) {
my $mid = $foo_node->getAttribute('mid');
my $inner_xml = join('', $foo_node->childNodes);
say "$mid $inner_xml";
}
|