从XPath获取兄弟变量值 - PHP

时间:2014-11-18 20:09:14

标签: php xpath

我使用以下内容从xtml中使用xpath提取数字:

$dom = new DOMDocument();
$dom->loadHTML('<span itemprop="priceCurrency" content="GBP"></span><span class="foo" d="bar" itemprop="price">£100</span>');
$xpath = new DOMXPath($dom);
$results = $xpath->query('//*[@itemprop="price"]');
$number = filter_var($results->item(0)->nodeValue, FILTER_SANITIZE_NUMBER_INT);
echo $number;

问题是,如何从content获取<span itemprop="priceCurrency" content="GBP">变量?

我可以使用以下内容进入节点:

$currency = $xpath->query('//*[@itemprop="priceCurrency"]');

但有没有办法获得content价值?

1 个答案:

答案 0 :(得分:1)

是的,使用getAttribute()方法:

$currency = $xpath->query('//*[@itemprop="priceCurrency"]')->item(0);
$content = $currency->getAttribute('content');
echo $content; // prints "GBP"