I'm trying to write some code to get a description from an XML document and print it out. Basically you pass in a city code like NCL and it gives you back Newcastle for example. Here is an example of my XML:
<locations>
<location>
<code>ABZ</code>
<description>Aberdeen</description>
</location>
...
But if you look at the xpath string in the following code, it does refer to the right place. What am I doing wrong? I can still refer to the rest of the dom can't I if I use [] in my xpath string?
<?php
function getDesc($code)
{
$answer = "";
$thedoc = new DOMDocument;
// Ignore whitespace between nodes (default: true)
$thedoc->preserveWhiteSpace = false;
// Load the XML data source
$thedoc->Load('flight.xml');
// Create a DOMXpath instance based on the
// DOMDocument object
$xpath = new DOMXPath($thedoc);
$qry = "/locations/location/code[. = \"".$code."\"]";
echo $qry;
// Execute the query and return a DOMNodeList
$entries = $xpath->query($qry);
foreach($entries as $entry)
{
$answer = $entry->parentNode->firstChild->nextSibling->nodeValue;
}
echo "<p />Answer ".$answer."<p />";
return $answer;
}//end of function
echo getDesc("NCL");
?>