开发者

xPath problem matching specific attribute

I've got the following HTML that I am trying to pull the state attribute out of.

<div id="content" class="listing mapListing" longitude="123.0000" latitude="-123.0000" listingName="business" 开发者_Python百科business=";12345678;;;;evar01=name" state="NSW" events="1">

I'm trying to match using the following PHP, which doesn't return "NSW".

$xpath = new DomXPath($html); 
    foreach ($xpath->query("//div[@id='content']") as $item) { 
              echo $item->getAttribute("state");
    }

However, the following; returns "listing" (instead of "listing mapListing") and my suspicion is the space is causing issues

$xpath = new DomXPath($html); 
        foreach ($xpath->query("//div[@id='content']") as $item) { 
                  echo $item->getAttribute("class");
        }

Does anyone have any suggestions on how I can match against "state" attribute?


Did you set the html namespace via xmlns in the document? Then PHP won't find any element unless you register the namespace:

$xpath = new DomXPath($html);
$xpath->registerNamespace('html', 'http://www.w3.org/1999/xhtml'); // or smth else
    foreach ($xpath->query("//html:div[@id='content']") as $item) { 
              echo $item->getAttribute("state");
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜