The DOM Model Page 38
March 15, 2002
Now we repeat this same pattern for the remainder of the nodes
under Travelpackage that we want to display as HTML. The second
XPath statement //Travelpackage[@name="a"]/* finds all
the children of Travelpackage where name="a" and
stores the result back into $tmpArray.
Again we print out the values to the browser by looping through
the array with a while statement:
$path = xpath_eval($context,
"//Travelpackage[@name=\"$var[$x]\"]/*");
$tmpArray = $path->nodeset;
while (list() = each($tmpArray)) {
$i++;
echo("<tr><td>");
echo($tmpArray[$i]->name);
echo("</td><td>");
echo($tmpArray[$i]->content);
echo("</td></tr>\n");
}
$i=0;
The next XPath statement //Travelpackage[@name="a"]/Package/*
finds all the children of Package where Travelpackage name =
"a". Again the nodes that are found when evaluating the XPath
statement are stored in $tmpArray. Rather than looping through
the array, we reference the elements of the array directly:
$path = xpath_eval($context,
"//Travelpackage[@name=\"$var[$x]\"]/Package/*");
$tmpArray = $path->nodeset;
echo("<tr><td>");
echo($tmpArray[0]->name);
echo("</td><td>");
echo($tmpArray[0]->content);
echo("</td></tr>\n");
echo("<tr><td>");
echo($tmpArray[1]->name);
echo("</td><td>");
echo($tmpArray[1]->content);
echo("</td></tr>\n");
echo("<tr><td colspan=2><hr></td></tr>\n");
}
?>
</table>
</body>
</html>
If the XML document was more than two conceptual recordsets long
then we would have to increase the size of the $var array to
account for the different size. Of course this means that we
have to know the size of the XML file. If you don't know the
size of the XML document or it varies in size then we need to
use a sizeof() function on $tmpArray.
This figure shows the output of the above code:
The DOM Model Page 37
Professional PHP4 Programming
The RAX Model Page 39
|