Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1130583
  • 博文数量: 113
  • 博客积分: 2422
  • 博客等级: 大尉
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-25 17:54
文章分类
文章存档

2016年(1)

2014年(8)

2013年(7)

2012年(13)

2011年(42)

2010年(26)

2009年(6)

2008年(6)

2007年(4)

我的朋友

分类: 系统运维

2012-03-25 17:39:00

Not sure I understand the question correctly, but the second XPath expression already does what you are describing. It does not match against the text node of the A element, but the href attribute:


点击(此处)折叠或打开

  1. $html = <<< HTML
  2. <ul>
  3.     <li>
  4.         <a href="">Description</a>
  5.     </li>
  6.     <li>
  7.         <a href="">Description</a>
  8.     </li>
  9. </ul>
  10. HTML;

  11. $xml = simplexml_load_string($html);
  12. $list = $xml->xpath("//a[contains(@href,'foo')]");


Outputs:


点击(此处)折叠或打开

  1. array(1) {
  2.   [0]=>
  3.   object(SimpleXMLElement)#2 (2) {
  4.     ["@attributes"]=>
  5.     array(1) {
  6.       ["href"]=>
  7.       string(31) ""
  8.     }
  9.     [0]=>
  10.     string(11) "Description"
  11.   }
  12. }


As you can see, the returned NodeList contains only the A element with href containing foo (which I understand is what you are looking for). It contans the entire element, because the XPath translates to Fetch all A elements with href attribute containing foo. You would then access the attribute with


点击(此处)折叠或打开

  1. echo $list[0]['href'] // gives ""

If you only want to return the attribute itself, you'd have to do


点击(此处)折叠或打开

  1. //a[contains(@href,'foo')]/@href

Note that in SimpleXml, this would return a SimpleXml element though:


点击(此处)折叠或打开

  1. array(1) {
  2.   [0]=>
  3.   object(SimpleXMLElement)#3 (1) {
  4.     ["@attributes"]=>
  5.     array(1) {
  6.       ["href"]=>
  7.       string(31) ""
  8.     }
  9.   }
  10. }


but you can output the URL now by


点击(此处)折叠或打开

  1. echo $list[0] // gives ""


阅读(2193) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~