用于查找嵌套元素的 Xpath 查询
Xpath query for finding nested element
我很难弄清楚为什么这个基本的两部分 xpath 示例没有得到任何结果,我已经使用 Chrome 的 XPath Helper 来确认第二个 xpath查询确实是正确的,并且 return 我正在寻找的行。
有人可以阐明为什么它会产生空结果吗?在此示例中,我希望获得 table 行,其中 BNY 作为货币代码,即中国的人民币。通常,我想要请求的任何货币代码的 table 行数据,或者如果货币代码不在 table 中则为空结果。
我也试过在 html 加载后立即执行第二个 xpath 查询(绕过 xpath 查询只检索 table),但那是 return 所有的行。我觉得我在这里做的事情从根本上是错误的,但我没能发现它,因为 Chrome 的 XPath Helper 工作正常。
$ccode = 'CNY';
// Create a DOM object
$html = new simple_html_dom();
$url = "http://www.xe.com/symbols.php";
// Load HTML from a URL
$html->load_file($url);
$table = $html->find('table.currencySymblTable', 0);
$xpathQuery = '//table/tbody/tr/td[2][text() = "'.$ccode.'"]/..';
echo $xpathQuery;
$tr = $table->find($xpathQuery);
print_r($tr);
你用错了currency code
应该是CNY你用错了
XPATH should be this table[@class="currencySymblTable"]/tr/td[2][text()="CNY"]/parent::tr
<?php
ini_set('display_errors', 1);
libxml_use_internal_errors(true);
$htmlContent = file_get_contents("http://www.xe.com/symbols.php");
$object = new DOMDocument();
$object->loadHTML($htmlContent);
$xpath = new DOMXPath($object);
$xpathQuery = '//table[@class="currencySymblTable"]/tr/td[2][text()="CNY"]/parent::tr';
$results = $xpath->query($xpathQuery);
foreach ($results as $result)
{
print_r($result);
}
输出:
DOMElement Object
(
[tagName] => tr
[schemaTypeInfo] =>
[nodeName] => tr
[nodeValue] => China Yuan RenminbiCNY¥¥165a5 info
[nodeType] => 1
[parentNode] => (object value omitted)
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] => (object value omitted)
[nextSibling] => (object value omitted)
[attributes] => (object value omitted)
[ownerDocument] => (object value omitted)
[namespaceURI] =>
[prefix] =>
[localName] => tr
[baseURI] =>
[textContent] => China Yuan RenminbiCNY¥¥165a5 info
)
我很难弄清楚为什么这个基本的两部分 xpath 示例没有得到任何结果,我已经使用 Chrome 的 XPath Helper 来确认第二个 xpath查询确实是正确的,并且 return 我正在寻找的行。 有人可以阐明为什么它会产生空结果吗?在此示例中,我希望获得 table 行,其中 BNY 作为货币代码,即中国的人民币。通常,我想要请求的任何货币代码的 table 行数据,或者如果货币代码不在 table 中则为空结果。
我也试过在 html 加载后立即执行第二个 xpath 查询(绕过 xpath 查询只检索 table),但那是 return 所有的行。我觉得我在这里做的事情从根本上是错误的,但我没能发现它,因为 Chrome 的 XPath Helper 工作正常。
$ccode = 'CNY';
// Create a DOM object
$html = new simple_html_dom();
$url = "http://www.xe.com/symbols.php";
// Load HTML from a URL
$html->load_file($url);
$table = $html->find('table.currencySymblTable', 0);
$xpathQuery = '//table/tbody/tr/td[2][text() = "'.$ccode.'"]/..';
echo $xpathQuery;
$tr = $table->find($xpathQuery);
print_r($tr);
你用错了currency code
应该是CNY你用错了
XPATH should be this
table[@class="currencySymblTable"]/tr/td[2][text()="CNY"]/parent::tr
<?php
ini_set('display_errors', 1);
libxml_use_internal_errors(true);
$htmlContent = file_get_contents("http://www.xe.com/symbols.php");
$object = new DOMDocument();
$object->loadHTML($htmlContent);
$xpath = new DOMXPath($object);
$xpathQuery = '//table[@class="currencySymblTable"]/tr/td[2][text()="CNY"]/parent::tr';
$results = $xpath->query($xpathQuery);
foreach ($results as $result)
{
print_r($result);
}
输出:
DOMElement Object
(
[tagName] => tr
[schemaTypeInfo] =>
[nodeName] => tr
[nodeValue] => China Yuan RenminbiCNY¥¥165a5 info
[nodeType] => 1
[parentNode] => (object value omitted)
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] => (object value omitted)
[nextSibling] => (object value omitted)
[attributes] => (object value omitted)
[ownerDocument] => (object value omitted)
[namespaceURI] =>
[prefix] =>
[localName] => tr
[baseURI] =>
[textContent] => China Yuan RenminbiCNY¥¥165a5 info
)