perl XML::LibXML 获取直接子文本节点内容

perl XML::LibXML get direct child text node content

喜欢这个片段:

<p>content 1 of p <span>content of span</span> content 2 of p </p>

我只想获取以下内容: content 1 of pcontent 2 of p,而不是 content of span

有办法吗?

使用 XPath:

for my $text_node ($node->findnodes('text()')) {
   say $text_node;
}

不使用 XPath:

for my $child_node ($node->childNodes()) {
   next if $child_node->nodeType != XML_TEXT_NODE;

   say $child_node;
}

两者都输出以下内容:

content 1 of p
 content 2 of p

程序的其余部分:

use strict;
use warnings;
use feature qw( say );

use XML::LibXML qw( XML_TEXT_NODE );

my $xml = '<p>content 1 of p <span>content of span</span> content 2 of p </p>';

my $doc = XML::LibXML->new->parse_string($xml);
my $node = $doc->documentElement();