Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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
php domxpath不使用concat函数_Php_Xpath - Fatal编程技术网

php domxpath不使用concat函数

php domxpath不使用concat函数,php,xpath,Php,Xpath,我从中解析url 使用xpath: concat( 'http://www.bodybuilding.com', //div[@class='product-details']/h3/a/@href) 但它不起作用 我试着提出其他问题 //`div[@class='product-details']/h3/a[concat('abc',@href)]` 那就行了 这个代码有什么问题 我正在使用 Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.19 D

我从中解析url

使用xpath:

concat( 'http://www.bodybuilding.com', //div[@class='product-details']/h3/a/@href)
但它不起作用

我试着提出其他问题

//`div[@class='product-details']/h3/a[concat('abc',@href)]` 
那就行了

这个代码有什么问题

我正在使用

Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.19
DOM/XML enabled
DOM/XML API Version 20031129
libxml Version  2.7.8
HTML Support    enabled
XPath Support   enabled
XPointer Support    enabled
Schema Support  enabled
RelaxNG Support enabled

将节点集传递到
concat()
无法按预期工作
concat()
要求其参数为字符串

下面的代码呢

$url = 'http://www.bodybuilding.com/store/accelerative/accelerative.htm';
$baseUrl = 'http://www.bodybuilding.com';

$doc = new DOMDocument();
@$doc->loadHTMLFile($url);
$selector = new DOMXPath($doc);

$xpath = '//div[@class="product-details"]/h3/a/@href';

foreach($selector->query($xpath) as $node) {
    var_dump($baseUrl . $node->nodeValue);
}
输出:

字符串(47)”http://www.bodybuilding.com/store/opt/whey.html"
字符串(47)”http://www.bodybuilding.com/store/opt/whey.html"
字符串(47)”http://www.bodybuilding.com/store/opt/whey.html"
字符串(47)”http://www.bodybuilding.com/store/opt/whey.html"
字符串(47)”http://www.bodybuilding.com/store/opt/whey.html"
字符串(47)”http://www.bodybuilding.com/store/opt/whey.html"
字符串(47)”http://www.bodybuilding.com/store/opt/whey.html"
字符串(47)”http://www.bodybuilding.com/store/opt/whey.html"
字符串(50)”http://www.bodybuilding.com/store/jym/pre-jym.html"
字符串(65)”http://www.bodybuilding.com/store/opt/essential-amino-energy.html"
字符串(65)”http://www.bodybuilding.com/store/opt/essential-amino-energy.html"
字符串(65)”http://www.bodybuilding.com/store/opt/essential-amino-energy.html"
字符串(65)”http://www.bodybuilding.com/store/opt/essential-amino-energy.html"
字符串(63)”http://www.bodybuilding.com/store/rsp-nutrition/quadralean.html"
字符串(63)”http://www.bodybuilding.com/store/rsp-nutrition/quadralean.html"
字符串(48)”http://www.bodybuilding.com/store/bsn/synth.html"
字符串(48)”http://www.bodybuilding.com/store/bsn/synth.html"
字符串(48)”http://www.bodybuilding.com/store/bsn/synth.html"
字符串(48)”http://www.bodybuilding.com/store/bsn/synth.html"
字符串(50)”http://www.bodybuilding.com/store/cellucor/c4.html"

您可以将节点集作为参数传递给
concat()
函数。然后,在XPath1.0中,会发生以下情况:如果节点集仅包含1个节点,则其字符串值将与所有其他参数连接。如果集合包含多个节点,则仅将第一个节点计算为其字符串值并连接。不使用语法就可以实现这一点是因为隐式类型转换。文档中说:
string,string[,string…]
你说得对,这是一种转换——但它并不是真正的副作用,在我看来,几乎所有字符串函数的这个属性都是经过深思熟虑的。但我的意思不同:目前,您的回答隐式地表示OP的XPath表达式不起作用,因为无法将节点集传递给
concat()
。从OPs尝试中,
concat('s)http://www.bodybuilding.com“,//div[@class='product-details']/h3/a/@href)
实际上是一个有效的XPath表达式,但将仅计算第一个
href
属性值
//div[@class='product-details']/h3/a[concat('abc',@href)]
检索所有这些内容,但谓词没有意义且总是正确的。@Timming现在你应该停止浪费别人的时间,先问一个不清楚的问题,不要对我的澄清请求做出反应,也不要发表令人费解的评论。思考片刻,决定不熟悉您的问题的人需要什么信息(平台、编程语言、您已有的代码…),让他人校对您的英语,正确标记您的问题。请编辑您的问题,并显示您希望在该页面上找到的确切信息。