XPath查询:从标签获取属性href [英] XPath Query: get attribute href from a tag

查看:94
本文介绍了XPath查询:从标签获取属性href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用XPath从 a -tag获取 href 属性,但它有两处出现在同一个文件中。我怎么相处?
我需要检查IF是否存在具有值$ street / object的 href 属性,我已经得到了这段代码,它不起作用:

I want to use XPath to get the href attribute from an a-tag, but it has two occurrences within the same file. How am I getting along? I need to check IF there is an href attribute with value $street/object, I have got this code and it does not work:

$product_photo     = $xpath->query("//a[contains(@href,'{$object_street}fotos/')][1]");
        $product_360       = $xpath->query("//a[contains(@href,'{$object_street}360-fotos/')][1]");
        $product_blueprint = $xpath->query("//a[contains(@href,'{$object_street}plattegrond/')][1]");
        $product_video     = $xpath->query("//a[contains(@href,'{$object_street}video/')][1]");

它根本不会返回任何东西。谁可以帮我解决问题?

It does not return anything at all. Who can help me out?

推荐答案

对于以下HTML文档:

For the following HTML document:

<html>
  <body>
    <a href="http://www.example.com">Example</a> 
    <a href="http://www.stackoverflow.com">SO</a> 
  </body>
</html>

xpath查询 / html / body // a / @ href (或简单地 // a / @ href )将返回:

The xpath query /html/body//a/@href (or simply //a/@href) will return:


    http://www.example.com
    http://www.stackoverflow.com

选择一个特定的实例使用 / html / body // a [N] / @ href

To select a specific instance use /html/body//a[N]/@href,


    $ /html/body//a[2]/@href
    http://www.stackoverflow.com

要测试属性中包含的字符串并返回属性本身,请将标记上的检查放在属性上:

To test for strings contained in the attribute and return the attribute itself place the check on the tag not on the attribute:


    $ /html/body//a[contains(@href,'example')]/@href
    http://www.example.com

混合两者:

Mixing the two:


    $ /html/body//a[contains(@href,'com')][2]/@href
    http://www.stackoverflow.com

这篇关于XPath查询:从标签获取属性href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆