1

I have some HTML like this:

<dd class="price">
    <sup class="symbol">&#36;</sup><span class="dollars">58</span><sup class="cents">.00</sup>
</dd>

What's the xpath to get $58.00 back as one string?

I'm using PHP:

$xpath = '?????';
$result = $xml->xpath($xpath);
echo $result[0];   // want this to show $58.00, possible?
4

3 回答 3

3

These are valid in your case, check for more detail the links below;

$html = '<dd class="price">
            <sup class="symbol">&#36;</sup><span class="dollars">58</span><sup class="cents">.00</sup>
         </dd>';
$dom = new DOMDocument();
$dom->loadXML($html);
$xpt = new DOMXpath($dom);
foreach ($xpt->query('//dd[@class="price"]') as $node) {
    // outputs: $58.00
    echo trim($node->nodeValue);
}
// or
$xml = new SimpleXMLElement($html);
$res1 = $xml->xpath('//dd[@class="price"]/sup');
$res2 = $xml->xpath('//dd[@class="price"]/span');
// outputs: $58.00
printf('%s%s%s', (string) $res1[0], (string) $res2[0], (string) $res1[1]);
  1. DOMDocument
  2. DOMXPath
  3. SimpleXMLElement
于 2013-01-27T23:38:54.513 回答
0

data()将返回当前上下文中的所有内容。尝试

//dd/data()
于 2013-01-27T09:37:21.530 回答
0

您还没有向我们展示您的代码,所以我不知道您使用的是什么平台。如果您有可以评估非节点 XPath 表达式的东西,那么您可以使用它:

string(//dd[@class = 'price'])

如果没有,您可以选择节点,

//dd[@class = 'price']

并且您正在使用的 API 应该具有获取所选节点的内部文本值的方法。

于 2013-01-27T09:45:10.140 回答