Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String HTML::TreeBuilder::XPath findvalue返回值的串联_String_Perl_Xpath_Concatenation_Html Treebuilder - Fatal编程技术网

String HTML::TreeBuilder::XPath findvalue返回值的串联

String HTML::TreeBuilder::XPath findvalue返回值的串联,string,perl,xpath,concatenation,html-treebuilder,String,Perl,Xpath,Concatenation,Html Treebuilder,中的findvalue函数返回由xpath查询找到的任何值的串联 为什么它会这样做,值的串联如何对任何人都有用 为什么会这样 调用findvalue时,您请求的是单个标量值。如果有多个匹配项,则必须以某种方式将它们组合成单个值 从以下文件中: findvalue($path) …如果路径返回节点集,则会自动为您调用$NodeSet->xpath\u to\u literal(从而返回Tree::XPathEngine::literal) 从以下文件中: xpath\u to\u literal(

中的
findvalue
函数返回由
xpath
查询找到的任何值的串联

为什么它会这样做,值的串联如何对任何人都有用

为什么会这样

调用
findvalue
时,您请求的是单个标量值。如果有多个匹配项,则必须以某种方式将它们组合成单个值

从以下文件中:

findvalue($path)

…如果路径返回节点集,则会自动为您调用
$NodeSet->xpath\u to\u literal
(从而返回Tree::XPathEngine::literal)

从以下文件中:

xpath\u to\u literal()

返回列表中所有节点的所有字符串值的串联

另一种方法是返回Tree::XPathEngine::NodeSet对象,这样用户就可以自己迭代结果,但是
findvalues
方法已经返回了一个列表


值的串联如何对任何人都有用

例如:

use strict;
use warnings 'all';
use 5.010;

use HTML::TreeBuilder::XPath;

my $content = do { local $/; <DATA> };
my $tree = HTML::TreeBuilder::XPath->new_from_content($content);

say $tree->findvalue('//p');

__DATA__
<p>HTML is just text.</p>
<p>It can still make sense without the markup.</p>
不过,通常情况下,获取匹配列表并对其进行迭代比执行哑连接更有意义,因此如果可以有多个匹配,则应该使用
findValue
(复数)

为什么会这样

调用
findvalue
时,您请求的是单个标量值。如果有多个匹配项,则必须以某种方式将它们组合成单个值

从以下文件中:

findvalue($path)

…如果路径返回节点集,则会自动为您调用
$NodeSet->xpath\u to\u literal
(从而返回Tree::XPathEngine::literal)

从以下文件中:

xpath\u to\u literal()

返回列表中所有节点的所有字符串值的串联

另一种方法是返回Tree::XPathEngine::NodeSet对象,这样用户就可以自己迭代结果,但是
findvalues
方法已经返回了一个列表


值的串联如何对任何人都有用

例如:

use strict;
use warnings 'all';
use 5.010;

use HTML::TreeBuilder::XPath;

my $content = do { local $/; <DATA> };
my $tree = HTML::TreeBuilder::XPath->new_from_content($content);

say $tree->findvalue('//p');

__DATA__
<p>HTML is just text.</p>
<p>It can still make sense without the markup.</p>
不过,通常情况下,获取匹配列表并对其进行迭代比执行哑连接更有意义,因此如果可以有多个匹配,则应使用
findValue
(复数)。

使用

( $tree->findvalues('//p') )[0] ;
相反。

使用

( $tree->findvalues('//p') )[0] ;

相反。

更明智的行为是返回标量上下文中的第一个匹配项和列表上下文中的所有匹配项,这是其他模块所做的,例如
HTML::Element中的
look\u
。否。它不应该返回你要求的以外的东西。如果你只想要第一个,就要第一个one@ikegami那么你是说
HTML::Element
中的
look\u down
是错误的吗?@ikegami那么你对
HTML::Element
中的
look\u down
有什么看法?它返回标量上下文中的第一个匹配项和列表上下文中的所有匹配项。@ikegami在对此答案的第二条注释中,您说它不应该这样做。更明智的行为是,它返回标量上下文中的第一个匹配项和列表上下文中的所有匹配项,这是其他模块所做的,例如,在
HTML::Element
中向下查看。它不应该返回你要求的以外的东西。如果你只想要第一个,就要第一个one@ikegami那么你是说
HTML::Element
中的
look\u down
是错误的吗?@ikegami那么你对
HTML::Element
中的
look\u down
有什么看法?它返回标量上下文中的第一个匹配项和列表上下文中的所有匹配项。@ikegami在对此答案的第二条注释中,您说过它不应该这样做。