2

I am having a difficult time figuring out why I'm not getting any result with this basic 2-part xpath example, and I've used Chrome's XPath Helper to confirm that the 2nd xpath Query is indeed correct and does return the row I am seeking. Can someone shed some light into why it's producing an empty result? In this example, I'm expecting to get the table row with BNY as the currency code, which is China's Yuan Renminbi. In general, I would want the table row data for any currency code that is requested, or an empty result if the currency code is not in the table.

I've also tried performing the 2nd xpath query immediately after the html load (bypassing the xpath query to retrieve JUST the table), but that's returning ALL the rows. I feel like there's something fundamentally wrong I'm doing here, but I haven't been able to discover it, as Chrome's XPath Helper works fine.

    $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);
3
  • can you tell use what you are trying to do..? Commented May 2, 2017 at 4:38
  • I've provided a couple more sentences to the end of my post to explain what I'm trying to do, thanks. Commented May 2, 2017 at 4:48
  • 1
    SimpleHtmlDom is not a real W3C DOM implementation. Different API and no Xpath (just a subset of CSS selectors). Commented May 4, 2017 at 7:12

1 Answer 1

1

You are using wrong currency code it should be CNY and you are using wrong

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);
}

Output:

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
)
Sign up to request clarification or add additional context in comments.

4 Comments

That's actually just a typo, CNY doesn't work either. Also tried /parent::tr instead of /.. at the end of the query, still comes up empty.
Yep, it works. I'm trying to use simple_html_dom, but that's fine, I don't really care. Thank you!
/.. at the end works as well. Not sure why simple_html_dom isn't playing well with xpath query, as I've used it in the past, but I just really need the end result, not how I achieved it. Thanks again.
@MikeT which end result?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.